SQL Left Join

SQL LEFT JOIN returns all the values from the left table with the matching rows in the right table. The result will be NULL in the right side when there is no match.


The Syntax of LEFT JOIN is

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

 

Or

 


SELECT column_name(s)
FROM table1
LEFT OUTER JOINtable2
ON table1.column_name=table2.column_name;

 

Example:

Let us consider two tables


1. Employee table

Id Employee_Name Empl_Age Monthly package
1 Sushil 22 18000
2 Bhargav 32 45000
3 Mohanthy 26 25000
4 Nithin 38 52000

 

2. Payment Table

Payment_ID Date Employee_Id Amount
150 23-02-2016 1 2500
151 30-06-2015 2 5000
152 14-09-2016 4 6500

 

Now we LEFT JOIN these tables


SELECT ID, Employee_Name, Amount, Date
FROM Employee
LEFT JOIN Payment
ON Employee.ID=Payment.Employee_Id;

 


The result will be displayed as follows

Id Employee_Name Amount Date
1 Sushil 2500 23-02-2016
2 Bhargav 5000 30-06-2015
3 Mohanthy NULL NULL
4 Nithin 6500 14-09-2016