Account
Categories

LEFT JOIN: Retrieve Unmatched Right Table Records


LEFT JOIN is an SQL operator used to retrieve all records from the left table and only match records from the right table.
If no match is found in the right table, then it returns NULL for the right table’s columns. In this process, the columns act as operands.

Syntax:

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;


Example:

Table1:  Employee

Employee_id	  name	    Department_id

1	          Mohan	            11
2	          Anaya	            12
3	          Babita	    13


Table2:   Department

Department_id	   Department_name

11	             HR
12	             IT
14	             Finance
   


SQL Query:


SELECT Employee.name, Department.department_name
FROM Employee
LEFT JOIN Department
ON Employee.department_id = Department.department_id;


Output:

Name  	        Department_name

Mohan	            HR
Anaya     	    IT
Babita	            NULL