SQL LEFT JOIN


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.


1. What is SQL LEFT JOIN?

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:

  • If a record in the left table has a matching record in the right table, the result will show the data from both tables.
  • If no match is found, the result will still show the data from the left table, but with NULL values for the right table’s columns.

2. Syntax of SQL LEFT JOIN

The syntax for an INNER JOIN is straightforward:

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
  • SELECT: Specifies the columns to retrieve.
  • FROM table1: Specifies the first table (left table).
  • LEFT JOIN table2: Specifies the second table (right table).
  • ON table1.column = table2.column: The condition for the join, usually based on related columns between the tables.

3. How LEFT JOIN Works

Let’s walk through an example to better understand how the LEFT JOIN works.

Consider two tables:

  1. employees: Stores employee details.
  2. departments: Stores department details.
-- 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        |
+---------------+--------------------+

LEFT JOIN Example:

SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;

Explanation:

  • The query retrieves employee details along with their department names.
  • It performs a LEFT JOIN between the employees table and the departments table using the department_id column.
  • All employees will be listed, but if there’s no department assignment (i.e., 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:

  • Alice, Bob, and David have departments, so their department_name values are displayed.
  • Charlie doesn’t have a department (because department_id is NULL), so the department_name is NULL.

4. Using LEFT JOIN for Filtering Data

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.

Example:

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:

  • This query retrieves employees who are not assigned to any department by checking where departments.department_name is NULL.
  • The result will show employees who have a NULL department_id.

Result:

employee_id employee_name
3 Charlie

5. LEFT JOIN vs. INNER JOIN

A common question is: how does a LEFT JOIN differ from an INNER JOIN?

  • INNER JOIN: Returns only rows where there is a match between the two tables.
  • LEFT JOIN: Returns all rows from the left table, regardless of whether there is a match in the right table.

Example of 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:

  • The INNER JOIN only returns employees who are assigned to a department. Charlie (who does not have a department) is excluded from the result.

6. Using LEFT JOIN with Multiple Tables

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:

  • The query returns all employees, their departments, and projects.
  • Employees without a department (like Charlie) or without an assigned project will show NULL in the respective columns.

7. Performance Considerations for LEFT JOIN

  • Indexes: Ensure the columns used in the ON clause are indexed to improve query performance, especially if the tables are large.
  • Join Conditions: Make sure the join condition is logical to avoid unnecessary data being processed.
  • Query Optimization: Use appropriate filtering and indexing to optimize performance, especially when dealing with large datasets.