0% found this document useful (0 votes)
53 views

Week 6 - SQL - Join

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Week 6 - SQL - Join

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SQL Join

Week 6
Join

• A SQL Join statement is used to combine data or rows from two or more tables
based on a common field between them. 
• Different types of Joins are:
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
CARTESIAN JOIN

• The CARTESIAN JOIN is also known as CROSS JOIN.


• In a CARTESIAN JOIN there is a join for each row of one table to every row of another
table. This usually happens when the matching column or WHERE condition is not
specified.
• In the absence of a WHERE condition the CARTESIAN JOIN will behave like a
CARTESIAN PRODUCT . i.e., the number of rows in the result-set is the product of the
number of rows of the two tables.
• In the presence of WHERE condition this JOIN will function like a INNER JOIN.
• Generally, Cross join is similar to an inner join where the join-condition will always
evaluate to True
• Syntax:
SELECT table1.column1 , table1.column2, table2.column1... FROM table1 CROSS
JOIN table2;
SELECT table1.column1 , table1.column2, table2.column1... FROM table1 ,table2;
Example of CARTESIAN JOIN
• In the below query we will select NAME and ID from Student table
and COURSE_ID and Course_Name from Course table.

• In the output, each row of the table Student is joined with every row of
the table Course. The total rows in the result-set = 5 * 3 = 15.

• SELECT Student.Id, Student.Name, Course.COURSE_Name ,


Course.COURSE_ID FROM Student CROSS JOIN Course;
SELF JOIN
• As the name signifies, in SELF JOIN a table is joined to itself. That is, each row of
the table is joined with itself and all other rows depending on some conditions.
• In other words we can say that it is a join between two copies of the same table.
• Syntax:
SELECT a.coulmn1 , b.column2 FROM table_name a, table_name b
WHERE some_condition;
some_condition: Condition for selecting the rows.

• Example Queries(SELF JOIN):


SELECT a.ROLL_NO , b.NAME FROM Student a, Student b WHERE
a.ROLL_NO < b.ROLL_NO;
INNER JOIN
• The simplest Join is INNER JOIN.
• The INNER JOIN, selects all rows from both the tables as long as the condition
satisfies. This will create the result-set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be same.
• Syntax:
SELECT table1.column1,table1.column2,table2.column1,.... FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;

matching_column: Column common to both the tables.

Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER JOIN.
Example Queries(INNER JOIN)

• This query will show the names and age of students enrolled in
different courses.

SELECT Course.COURSE_ID, Student.NAME, Student.AGE FROM


Student
INNER JOIN Course
ON Student.ROLL_NO = Course.ROLL_NO;
LEFT JOIN
• This join returns all the rows of the table on the left side of the join and matching rows for the table
on the right side of join. The rows for which there is no matching row on right side, the result-set
will contain null.
• LEFT JOIN is also known as LEFT OUTER JOIN.
• Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

• table1: First table.


• table2: Second table
• matching_column: Column common to both the tables.
Example Queries(LEFT JOIN):

SELECT Student.NAME,Course.COURSE_ID
FROM Student
LEFT JOIN Course
ON Course.ROLL_NO = Student.ROLL_NO;
RIGHT JOIN:
• RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the
table on the right side of the join and matching rows for the table on the
left side of join. The rows for which there is no matching row on left side,
the result-set will contain null.
• RIGHT JOIN is also known as RIGHT OUTER JOIN.
• Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
matching_column: Column common to both the tables.
• Note: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both
are same.
Example Queries(RIGHT JOIN):

SELECT Student.NAME,Course.COURSE_ID
FROM Student
RIGHT JOIN Course
ON Course.ROLL_NO = Student.ROLL_NO;
FULL JOIN:
• FULL JOIN creates the result-set by combining result of both LEFT JOIN and RIGHT JOIN. The result-
set will contain all the rows from both the tables. The rows for which there is no matching, the result-set
will contain NULL values.

• Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;

• table1: First table.


• table2: Second table
• matching_column: Column common to both the tables.
Example Queries(FULL JOIN):

SELECT Student.NAME,Course.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON Course.ROLL_NO = Student.ROLL_NO;
Summary

You might also like