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
ONtable1.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

IdEmployee_NameEmpl_AgeMonthly package
1Sushil2218000
2Bhargav3245000
3Mohanthy2625000
4Nithin3852000

 

2. Payment Table

Payment_IDDateEmployee_IdAmount
15023-02-201612500
15130-06-201525000
15214-09-201646500

 

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

IdEmployee_NameAmountDate
1Sushil250023-02-2016
2Bhargav500030-06-2015
3MohanthyNULLNULL
4Nithin650014-09-2016