Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
612 views

SQL Joins

SQL Joins allow combining data from multiple tables. The main join types are: - INNER JOIN returns rows when there is a match in both tables - LEFT JOIN returns all rows from the left table, and matched rows from the right table - RIGHT JOIN returns all rows from the right table, and matched rows from the left table - FULL JOIN returns all rows and fills in null values when there is no match

Uploaded by

Teshale Siyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
612 views

SQL Joins

SQL Joins allow combining data from multiple tables. The main join types are: - INNER JOIN returns rows when there is a match in both tables - LEFT JOIN returns all rows from the left table, and matched rows from the right table - RIGHT JOIN returns all rows from the right table, and matched rows from the left table - FULL JOIN returns all rows and fills in null values when there is no match

Uploaded by

Teshale Siyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

SQL JOINS

SQL | Join (Inner, Left, Right and Full Joins)

• 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:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN

• Consider the two tables below:


CONT…
STUDENT
COURSE
SID Name Sex Age Phone
1 Ali Mohammed M 20 xxx- xxx-xxxx CourseID SID
2 Hirut Zerfu F 21 xxx- xxx-xxxx 1 1
3 Lia Maru F 20 xxx- xxx-xxxx 2 2
4 Ewnetu Zeberga M 22 xxx- xxx-xxxx 2 3
5 Memhiru Lukas M 21 xxx- xxx-xxxx 3 4
6 Ethiopia Hagere F 22 xxx- xxx-xxxx 1 5
7 Antony Kibru M 20 xxx- xxx-xxxx 4 9
8 Mesay Legesse M 19 xxx- xxx-xxxx 5 10
4 11
1. INNER JOIN
 The INNER JOIN keyword selects all rows from both the tables as long as the condition satisfies.
 This keyword 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;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.
Cont…
Note: We can also write JOIN instead of INNER JOIN. JOIN is same
as INNER JOIN.

Fig.1 illustration of INNER JOIN


Cont…
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.SID=Course.SID;
Cont…
SI Name CourseID SID
D 1 1
1 Ali Mohammed 2 2
2 Hirut Zerfu
1 2 3
3 Lia Maru 2
Student 3 4
4 Ewnetu Zeberga 3 Course
5 Memhiru Lukas 4 1 5
6 Ethiopia Hagere 5 4 9
7 Antony Kibru 5 10
8 Mesay Legesse 4 11

The Matching Output 1,2,3,4,5


Cont…
The Output
2. 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.
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.
Cont…
• Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN,
both are same.

Fig.2 Illustration of Left Outer Join


Example Queries(LEFT JOIN):

SELECT Student.NAME,Course.COURSE_ID
FROM Student
LEFT JOIN Course
ON Course.SID = Student.SID;

Output Of Left Join Above


Cont…

Student Course
Cont…
Output Of Left Join Above
Name CourseID

Ali Mohammed 1

Hirut Zerfu 2

Lia Maru 2

Ewnetu Zeberga 3

Memhiru Lukas 1

Ethiopia Hagere Null

Antony Kibru Null

Mesay Legesse Null


3. Right 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.
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.
Cont…

Course
Student
Cont…

SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;

Name CourseID
Cont…
Output Of Above Right Join Query
Name CourseID
Ali Mohammed 1
Hirut Zerfu 2
Lia Maru 2
Ewnetu Zeberga 3
Memhiru Lukas 1
Null 4
Null 5
Null 4
4. 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.
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.
Cont…

SELECT Student.NAME,Course.COURSE_ID
Student Course FROM Student
FULL JOIN Course
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Cont…
Name CourseID
Ali Mohammed 1
Hirut Zerfu 2
Lia Maru 2
Ewnetu Zeberga 4
Memhiru Lukas 5
Ethiopia Hager Null
Antony Kibru Null
Mesay Legesse Null
Null 4
Null 5
Null 4
Thank You!

You might also like