LEFT JOIN
Filtering Pitfall
Learn why placing a filter on a LEFT JOINed table in the WHERE clause silently converts your query into an INNER JOIN, and how to avoid it.
What is a LEFT JOIN?
A LEFT JOIN (or LEFT OUTER JOIN) returns all rows from the left table, plus matching rows from the right table. If there is no match, the right-side columns are filled with NULL.
The NULL Contract
When a left-side row has no match in the right table, every right-side column becomes NULL. This is exactly why you chose LEFT JOIN: to preserve those unmatched rows.
| name | order_id |
|---|---|
| Alice | 101 |
| Alice | 102 |
| Bob | 103 |
| Charlie | NULL |
The WHERE Clause Trap
What happens if you add a WHERE filter on a column from the right (LEFT JOINed) table?
Silent Conversion to INNER JOIN
Execution Order is the Key
SQL does not run top-to-bottom like code. The database engine processes clauses in a specific logical order:
ON vs WHERE: The Key Distinction
| Filter in ON | Filter in WHERE | |
|---|---|---|
| Unmatched left rows | Preserved (with NULLs) | Eliminated |
| Behavior | True LEFT JOIN | Becomes INNER JOIN |
| Right-side NULLs | Kept in result | Fail the test |
The Fix: Move the Filter to ON
To filter on the right table without losing unmatched rows, move the condition into the ON clause:
How to Apply the Rule
Example 1: Students and Grades
Find all students and their math grades. Students without a math grade should still appear.
Example 2: Products and Reviews
List all products with their 5-star reviews. Products without any 5-star review should show NULL.
| name | text |
|---|---|
| Widget A | Amazing product! |
| Widget B | NULL |
| Widget C | NULL |
Example 3: The IS NULL Exception
Sometimes you want to filter on a right-table column in WHERE: specifically, to find rows that have no match at all.
Example 4: Mixed Conditions
What if you need conditions on both the left table and the right table?
Alternative: Pre-Filter with a Subquery
Another approach: filter the right table before the join happens, using a subquery or CTE.
Common Mistakes
Summary Decision Table
| Scenario | Correct Placement | Pitfall if Wrong |
|---|---|---|
| Filter on left table | WHERE | N/A (always safe) |
| Filter on right table (keep unmatched) | ON clause | Becomes INNER JOIN |
| Find rows with no match | WHERE ... IS NULL | N/A (IS NULL is safe) |
| Complex right-table logic | Subquery + LEFT JOIN | Loses unmatched rows |
| Filter on both tables | ON for right, WHERE for left | Mixed results |
Question 1 of 5
Given this query:
SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.id
WHERE d.location = 'NYC';
What happens to employees who are not assigned to any department?
Question 2 of 5
Which clause should you use to filter a RIGHT-table column while preserving all left rows?
Question 3 of 5
Query A:
SELECT * FROM A LEFT JOIN B ON A.id = B.a_id AND B.status = 'active';
Query B:
SELECT * FROM A LEFT JOIN B ON A.id = B.a_id WHERE B.status = 'active';
Which statement about Query A and Query B is true?
Question 4 of 5
Which of these WHERE conditions on a LEFT JOINed right table does NOT cause the pitfall?
Question 5 of 5
You need to list all authors and their published books (status = 'published'). Authors with no published books should appear with NULL. Which approach is correct?