LEFT JOIN
Preserving Every Row
Learn how to combine tables while keeping all rows from your primary table: even when there is no match on the other side.
What Is a JOIN?
A JOIN combines rows from two or more tables based on a related column between them. Think of it like matching entries from two spreadsheets using a shared ID.
The Problem LEFT JOIN Solves
Imagine a school with a students table and an enrollments table. Some students have not enrolled in any course yet. A regular INNER JOIN would drop those students from the results entirely.
Visualizing LEFT JOIN
LEFT JOIN Syntax
FROM left_table
LEFT JOIN right_table
ON left_table.key \(=\) right_table.key;
How LEFT JOIN Works
LEFT JOIN vs INNER JOIN
| LEFT JOIN | INNER JOIN | |
|---|---|---|
| Unmatched left rows | Kept (with NULLs) | Dropped |
| Matched rows | Returned normally | Returned normally |
| Result row count | \(\geq\) left table rows | Can be more or less |
| Use case | "Show all students" | "Show enrolled students" |
| NULL handling | Right columns may be NULL | No NULLs from join |
Understanding NULL in Results
When a left-table row has no match, every column from the right table is filled with NULL. This is not an error: it is information telling you "no corresponding data exists."
How to Write a LEFT JOIN
Example 1: Students and Courses
FROM students s
LEFT JOIN enrollments e
ON s.id \(=\) e.student_id;
| name | course |
|---|---|
| Alice | Math |
| Alice | Art |
| Bob | NULL |
| Carol | Science |
Example 2: Finding Unmatched Rows
A powerful pattern: use LEFT JOIN + WHERE ... IS NULL to find rows in the left table that have no match in the right table.
FROM students s
LEFT JOIN enrollments e
ON s.id \(=\) e.student_id
WHERE e.student_id IS NULL;
| name |
|---|
| Bob |
Example 3: LEFT JOIN with COUNT
Count how many courses each student is enrolled in: including students with zero enrollments.
COUNT(e.course) AS course_count
FROM students s
LEFT JOIN enrollments e
ON s.id \(=\) e.student_id
GROUP BY s.name;
| name | course_count |
|---|---|
| Alice | 2 |
| Bob | 0 |
| Carol | 1 |
ON vs WHERE with LEFT JOIN
Placing a filter condition in ON versus WHERE produces different results with LEFT JOIN. This is a subtle but critical distinction.
Chaining Multiple LEFT JOINs
You can LEFT JOIN more than one table. Each additional join preserves the rows from the left side of that specific join.
FROM students s
LEFT JOIN enrollments e
ON s.id \(=\) e.student_id
LEFT JOIN grades g
ON e.id \(=\) g.enrollment_id;
Common Mistakes
JOIN Types Summary
| JOIN Type | Left Rows | Right Rows | Unmatched |
|---|---|---|---|
| INNER JOIN | Only matched | Only matched | Dropped |
| LEFT JOIN | All kept | Only matched | NULL-filled |
| RIGHT JOIN | Only matched | All kept | NULL-filled |
| FULL OUTER JOIN | All kept | All kept | NULL-filled |
Question 1 of 5
In a LEFT JOIN, what happens to rows in the left table that have no matching row in the right table?
Question 2 of 5
Which query correctly finds customers who have never placed an order?
Question 3 of 5
Given 5 rows in the left table and 3 matching rows in the right table, what is the minimum number of rows a LEFT JOIN can return?
Question 4 of 5
You write: SELECT * FROM A LEFT JOIN B ON A.id \(=\) B.a_id WHERE B.status \(=\) 'active'; What is the problem?
Question 5 of 5
When using LEFT JOIN with COUNT, why should you write COUNT(right_table.column) instead of COUNT(*)?