In SQL, joins are essential for combining data from multiple tables. One of the most useful types of joins is the LEFT JOIN, also known as the LEFT OUTER JOIN. This join type allows you to retrieve all records from the left table and the matching records from the right table. If there’s no match, the result will include NULL
for columns from the right table.
A LEFT JOIN (or LEFT OUTER JOIN) returns all records from the left table (the first table in the query), and the matching rows from the right table (the second table). If there’s no match in the right table, the result will include NULL
for the right table’s columns.
In simpler terms:
NULL
values for the right table’s columns.The syntax for an INNER JOIN is straightforward:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Let’s walk through an example to better understand how the LEFT JOIN works.
Consider two tables:
-- employees table
+-------------+-----------------+---------------+
| employee_id | employee_name | department_id |
+-------------+-----------------+---------------+
| 1 | Alice | 1 |
| 2 | Bob | 2 |
| 3 | Charlie | NULL |
| 4 | David | 3 |
+-------------+-----------------+---------------+
-- departments table
+---------------+--------------------+
| department_id | department_name |
+---------------+--------------------+
| 1 | HR |
| 2 | Finance |
| 3 | Engineering |
+---------------+--------------------+
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
Explanation:
employees
table and the departments
table using the department_id
column.department_id
is NULL
), the department_name
will be NULL
.Result:
employee_id | employee_name | department_name |
---|---|---|
1 | Alice | HR |
2 | Bob | Finance |
3 | Charlie | NULL |
4 | David | Engineering |
Explanation of Result:
department_name
values are displayed.department_id
is NULL
), so the department_name
is NULL
.You can also use the LEFT JOIN to filter data by including conditions in the WHERE
clause. For example, you can find employees who don't belong to any department.
SELECT employees.employee_id, employees.employee_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id
WHERE departments.department_name IS NULL;
Explanation:
departments.department_name
is NULL
.NULL
department_id
.Result:
employee_id | employee_name |
---|---|
3 | Charlie |
A common question is: how does a LEFT JOIN differ from an INNER JOIN?
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
Result for INNER JOIN:
employee_id | employee_name | department_name |
---|---|---|
1 | Alice | HR |
2 | Bob | Finance |
4 | David | Engineering |
Explanation:
You can join more than two tables using the LEFT JOIN to retrieve data from multiple related tables. For example, consider a projects
table that contains project assignments for employees.
-- projects table
+------------+-------------+----------------+
| project_id | project_name| employee_id |
+------------+-------------+----------------+
| 1 | Project X | 1 |
| 2 | Project Y | 2 |
| 3 | Project Z | 4 |
+------------+-------------+----------------+
To retrieve employee details along with their department and project names, you can perform a LEFT JOIN on three tables:
SELECT employees.employee_name, departments.department_name, projects.project_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id
LEFT JOIN projects ON employees.employee_id = projects.employee_id;
Result:
employee_name | department_name | project_name |
---|---|---|
Alice | HR | Project X |
Bob | Finance | Project Y |
Charlie | NULL | NULL |
David | Engineering | Project Z |
Explanation:
NULL
in the respective columns.ON
clause are indexed to improve query performance, especially if the tables are large.