The LEFT JOIN keyword returns all the records from the left table and matching records from the right table.
Syntax:
SELECT column_name(s) FROM left_table
LEFT JOIN right_table
ON left_table.column_name = right_table.column_name;
LEFT (OUTER) JOIN
DEMO TABLES
STUDENTS TABLE
Roll_No | Name | Marks |
1 | Dev | 35 |
2 | Ayush | 45 |
3 | Ram | 55 |
4 | Pyush | 65 |
5 | Nazim | 75 |
CLG TABLE
Id | Phone_no |
1 | 946547846 |
2 | 854547845 |
3 | 756547841 |
4 | 846447842 |
5 | 874647841 |
10 | 985647841 |
11 | 964747841 |
12 | 765447841 |
Example: To perform left join on both the above tables
SELECT Roll_No, Name FROM students
INNER JOIN clg
ON students.Roll_No = clg.Roll_No;
The resultant table will be as shown below:
Roll_No | Name | Marks | Id | Phone_no |
1 | Dev | 35 | 1 | 946547846 |
2 | Ayush | 45 | 2 | 854547845 |
3 | Ram | 55 | 3 | 756547841 |
4 | Pyush | 65 | 4 | 846447842 |
5 | Nazim | 75 | 5 | 874647841 |
6 | ABC | 75 | NULL | NULL |
7 | ABC | 75 | NULL | NULL |
8 | Nazim | 75 | NULL | NULL |