SQL Left Join
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.
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
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 employee.dept_no=department.dept_no;
The output that we will get is as follows :-
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.