01 / 21

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.

SQL

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.

Left Table Right Table Match LEFT JOIN returns everything shaded

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.

SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON c.id = o.customer_id;
nameorder_id
Alice101
Alice102
Bob103
CharlieNULL

The WHERE Clause Trap

What happens if you add a WHERE filter on a column from the right (LEFT JOINed) table?

SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.amount > 50; -- DANGER!

Silent Conversion to INNER JOIN

LEFT JOIN (intended) All Match +WHERE on right table INNER JOIN (actual) Only Returned rows Unmatched rows lost

Execution Order is the Key

SQL does not run top-to-bottom like code. The database engine processes clauses in a specific logical order:

1
FROM + JOIN: tables are combined, NULLs inserted for unmatched rows
2
WHERE: filters applied to the combined result (NULLs fail here)
3
GROUP BY: grouping happens on surviving rows
4
SELECT: columns chosen for output

ON vs WHERE: The Key Distinction

ON clause
Runs during the join. Non-matching right rows become NULL but the left row stays.
WHERE clause
Runs after the join. NULL values fail the test, so the left row is removed.
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:

-- ✕ WRONG: loses unmatched rows SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.amount > 50; -- ✓ CORRECT: preserves all left rows SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON c.id = o.customer_id AND o.amount > 50;

How to Apply the Rule

1
Identify your join type. Are you using LEFT JOIN? If yes, proceed.
2
Check every WHERE condition. Does it reference a column from the right table?
3
Ask: will NULL pass this test? If no (and it almost never will), you have the pitfall.
4
Move the condition from WHERE into the ON clause using AND.
5
Exception: If you explicitly want to exclude unmatched rows (e.g., WHERE o.id IS NULL to find non-matches), WHERE is correct.

Example 1: Students and Grades

Find all students and their math grades. Students without a math grade should still appear.

-- ✕ WRONG: students without math grade disappear SELECT s.name, g.score FROM students s LEFT JOIN grades g ON s.id = g.student_id WHERE g.subject = 'Math';
-- ✓ CORRECT: all students appear SELECT s.name, g.score FROM students s LEFT JOIN grades g ON s.id = g.student_id AND g.subject = 'Math';

Example 2: Products and Reviews

List all products with their 5-star reviews. Products without any 5-star review should show NULL.

-- ✕ WRONG: unreviewed products vanish SELECT p.name, r.text FROM products p LEFT JOIN reviews r ON p.id = r.product_id WHERE r.stars = 5;
-- ✓ CORRECT SELECT p.name, r.text FROM products p LEFT JOIN reviews r ON p.id = r.product_id AND r.stars = 5;
nametext
Widget AAmazing product!
Widget BNULL
Widget CNULL

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.

-- ✓ This is CORRECT usage of WHERE with LEFT JOIN SELECT c.name FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.customer_id IS NULL;

Example 4: Mixed Conditions

What if you need conditions on both the left table and the right table?

-- Left-table filter: stays in WHERE (safe) -- Right-table filter: goes in ON SELECT c.name, o.total FROM customers c LEFT JOIN orders o ON c.id = o.customer_id AND o.status = 'shipped' -- right table: ON WHERE c.country = 'US'; -- left table: WHERE
Left-table filter
Safe in WHERE: the left table has no NULLs from the join.
Right-table filter
Must go in ON: otherwise NULLs are eliminated.

Alternative: Pre-Filter with a Subquery

Another approach: filter the right table before the join happens, using a subquery or CTE.

SELECT c.name, big_orders.order_id FROM customers c LEFT JOIN ( SELECT customer_id, order_id FROM orders WHERE amount > 50 ) big_orders ON c.id = big_orders.customer_id;

Common Mistakes

✕ WHERE on right column
Any comparison on a LEFT JOINed column in WHERE (except IS NULL) kills unmatched rows.
✕ Using OR with right column
WHERE o.status = 'A' OR o.status = 'B' still fails for NULLs. Move both into ON.
✕ NOT IN with NULLs
WHERE o.type NOT IN ('X','Y') fails for NULL. NULL NOT IN (...) is UNKNOWN, not TRUE.
✕ Functions on right column
WHERE YEAR(o.date) = 2024 also fails: YEAR(NULL) is NULL, which is filtered out.

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

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

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?