SQL LEFT JOIN


The LEFT JOIN returns all the values from the left table and it also includes matching values from right table, if there are no matching join value it returns NULL.

Syntax:
SELECT table1.column1, table2.column2...
FROM table1
LEFT JOIN table2
ON table1.common_field = table2.common_field;

Example

customer Table

idnameagesalary
1Ashwani515000
2Sachin215000
3Soun241000
4Deepak232000
5Ravi232000

order Table

o_iddatecust_idamount
00120-02-201523000
00212-04-201522000
00322-06-201534000
00411-08-201545000
SELECT id, name, amount,date
FROM customer
LEFT JOIN order
ON customer.id = order.cust_id;
Output
idnameamountdate
1AshwaniNULLNULL
2Sachin300020-02-2015
2Sachin200012-04-2015
3Soun400022-06-2015
4Deepak500011-08-2015
5RaviNULLNULL

Share this article on