A LEFT JOIN (or LEFT OUTER JOIN) returns all rows from the left table and the matching rows from the right table.
If there is no match in the right table, the result will show NULL for the right table columns.
Basic syntax:
Step 1: Example tables
Employees Table
| EmployeeID | FirstName | LastName | DepartmentID |
|---|---|---|---|
| 1 | John | Doe | 101 |
| 2 | Jane | Smith | 102 |
| 3 | Mike | Brown | 101 |
| 4 | Sara | White | NULL |
Departments Table
| DepartmentID | DepartmentName |
|---|---|
| 101 | IT |
| 102 | HR |
| 103 | Finance |
Step 2: Simple LEFT JOIN
Retrieve all employees with their department names, including employees without a department:
Result:
| FirstName | LastName | DepartmentName |
|---|---|---|
| John | Doe | IT |
| Jane | Smith | HR |
| Mike | Brown | IT |
| Sara | White | NULL |
Sara’s DepartmentID is
NULL, so DepartmentName showsNULL.
IT and HR employees are matched correctly.
Finance department is not included because it has no employees.
Step 3: LEFT JOIN with table aliases
- e → Employees
- d → Departments
Result: Same as Step 3.
Table aliases make queries cleaner and easier to read, especially with multiple tables.
Step 4: LEFT JOIN with WHERE clause
You can filter results after performing the join:
Result:
| FirstName |
|---|
| Sara |
Shows employees without a department.
Important: If you filter right table columns, useIS NULLcarefully; otherwise, you might unintentionally convert LEFT JOIN into INNER JOIN.
Step 5: LEFT JOIN with multiple conditions
You can add more conditions to the ON clause:
Result:
| FirstName | LastName | DepartmentName |
|---|---|---|
| John | Doe | IT |
| Mike | Brown | IT |
| Jane | Smith | NULL |
| Sara | White | NULL |
Only IT departments are matched; others show
NULL.
