Using Join

Written by

Soumya Shaw

The Join Function works as same as the Name suggests. It uses the data of two tables to show the relevant information that the User needs. Even though, the actual syntax doesn’t use the keyword “Join” but can denote the same result.

We will be adding up a relevant table in the Database so that we can understand the working of Joining of Tables.

CREATE TABLE sports(
Roll INT PRIMARY KEY NOT NULL, Name VARCHAR(20) NOT NULL,
Sports_Grade CHAR(1), Sports VARCHAR(20));

INSERT INTO sports (
roll,name,sports_grade,sports)
VALUES (
18,”Soumya”,”A”,”Cricket”);
INSERT INTO sports (
roll,name,sports_grade,sports)
VALUES (
15,”Meena”,”B”,”Volleyball”);
INSERT INTO sports (
roll,name,sports_grade,sports)
VALUES (
11,”Laksith”,”S”,”Football”);
INSERT INTO sports (
roll,name,sports_grade,sports)
VALUES (
7,”Himanshu”,”S”,”Basketball”);
INSERT INTO sports (
roll,name,sports_grade,sports)
VALUES (
6,”Gungun”,”A”,”Skipping”);

Now, go through the Query that will look an Alien to you because some of its components but soon will get clarified in later stages.

SELECT a.name, a.dob, a.marks, b.sports
FROM school a, sports b
WHERE a.name=b.name;

Syntax Analysis: There might be some new things that just appeared onto the Syntax. So, let’s break it up into simpler modules. The letters a, b that are used denotes the specific tables that are renamed just for that query and symbolizes which table’s column to follow when the same name is used. The last WHERE condition manages to eliminate out the other cross products that we don’t need and shows only the common data.

Let’s go to the next Query and understand it in a better way.

SELECT a.name, a.marks, b.sports, b.sports_grade
FROM school a, sports b
WHERE a.name=b.name
AND marks>50;

Okay, now a tricky part for you to self-evaluate yourself. There is a query that is used here and your task is to find out why there is a Duplicity in the Output when we use the following condition. Do use the Precious Internet Resource for that!

SELECT a.roll, a.name, a.grade, b.sports_grade
FROM school a, sports b
WHERE a.grade=b.sports_grade;

Using Join