Skip to content
Related Articles

Related Articles

Improve Article
SQL Left Join
  • Last Updated : 26 Apr, 2021

The LEFT JOIN keyword in SQL returns the all matching records(or rows) and the records(or rows) that are present in the left table but not in the right table. That means that, if a certain row is present in the left table but not in the right, the result will include this row but with a NULL value in each column from the right. If a record from the right table is not in the left, it will not be included in the result.

LEFT JOIN

The syntax for a LEFT JOIN is :

SELECT column_name(s) 
FROM tableA 
LEFT JOIN tableB ON tableA.column_name = tableB.column_name;

SQL LEFT JOIN EXAMPLE :

In this example we will consider two tables Employee containing details of the employees working in the particular department the and department table containing the details of the department

employee table



emp_noemp_nameagesalarydept_no
E1Varun Singhal2730,000D1
E2Amrita Aggarwal2825,000D2
E3Ravi Anand3034,000D1
E4Nitin Saini3454,000[NULL]
E5Muskan Garg3565,000[NULL]

department table 

dept_nodept_namelocation
D1ITDelhi
D2HRHyderabad
D3FINANCERajasthan

To perform left- join on  these two tables we will use the following SQL query :

select emp_no ,emp_name , age, salary ,dept_name, location 
from employee 
left join department on empolyee.dept_no=department.dept_no;

The output that we will get is as follows :-

emp_noemp_nameagesalarydept_namelocation
E1Varun Singhal2730,000ITDelhi
E3Ravi Anand3034,000ITDelh
E2Amrita Singhal2825,000HRHyderabad
E4Nitin Saini3454,000[NULL][NULL]
E5Muskan Garg3565,000[NULL][NULL]

As left join gives the matching rows and the rows that are present in the left table but not in the right table. Here in this example, we see that the employees that does not work in a particular department, i.e, having dept no values as [NULL], contains [NULL] values of dept name and location after the left join.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :