Filtering in ON Clause vs WHERE Clause in Outer Joins
Put predicates in ON vs WHERE correctly so outer joins stay outer.
In plain words
ON says how to match rows. WHERE throws rows away after matching. Put conditions about the optional table in ON if you want to keep everyone.
Think of it like…
Inviting every employee to a lunch (LEFT JOIN) and noting who's vegetarian. ON is the rule for pairing people with meal choices. WHERE is a bouncer at the door afterwards: tell him "only vegetarians" and everyone without a recorded choice gets turned away too.
Where you’ve seen it
"Every customer with their November orders, including customers with none": FROM customers c LEFT JOIN orders o ON o.customer_id = c.id AND o.created_at >= '2026-11-01' keeps everyone. Moving the date condition to WHERE drops every customer without a November order.
Words you’ll hear
- ON condition
- Decides which right rows pair with each left row.
- WHERE condition
- Filters rows after the join is built.
- Preserved table
- The side an outer join keeps in full (the left table in LEFT JOIN).
- Null-rejecting filter
- A WHERE condition that's never true for NULL, which removes NULL-padded rows.
Visual diagram
Theory & example queries
Right-table conditions: ON keeps, WHERE removes
A LEFT JOIN first pairs each left row with right rows that satisfy ON, padding with NULLs when none do. WHERE then filters that result. A WHERE condition on a right-table column, like o.status = 'paid', evaluates to UNKNOWN on NULL-padded rows, so those rows (the customers with no paid orders) are removed.
Put the condition in ON and it only restricts which orders can match: customers without paid orders still appear, with NULL order columns. The one deliberate use of a right-table condition in WHERE is the anti-join, WHERE o.id IS NULL, which keeps exactly the unmatched rows.
| id | name | city | |
|---|---|---|---|
| 1 | Maya | maya@example.com | Pune |
| 2 | Ravi | ravi@example.com | Delhi |
| 3 | Noor | noor@example.com | Mumbai |
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT c.id, COUNT(o.id)FROM customers c LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'paid'GROUP BY c.id;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative paid-order counts3 sample rows
| id | count |
|---|---|
| 7 | 2 |
| 8 | 0 |
| 9 | 1 |
Note: Sample data assumes customer 8 has no paid order; the LEFT JOIN still preserves that customer and COUNT(o.id) returns zero.
Key takeaway: Filter the optional (right) side in ON.
Left-table conditions, and chains of joins
The reverse surprise: a left-table condition in ON, such as ON o.customer_id = c.id AND c.country = 'IN', doesn't remove non-Indian customers. They're still returned, just without any matched orders, because a LEFT JOIN keeps every left row no matter what ON says. To filter the left table, use WHERE.
In multi-join queries, an INNER JOIN (or a WHERE filter) on a table that was LEFT-joined earlier also discards the NULL-padded rows, undoing the outer join. Either make the later joins LEFT JOINs too, or group the optional tables with parentheses or a subquery.
| id | name | city | |
|---|---|---|---|
| 1 | Maya | maya@example.com | Pune |
| 2 | Ravi | ravi@example.com | Delhi |
| 3 | Noor | noor@example.com | Mumbai |
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT c.id, o.idFROM customers c LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'paid'WHERE c.country = 'IN';PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative Indian customers3 sample rows
| c.id | o.id |
|---|---|
| 7 | 901 |
| 7 | 908 |
| 8 | NULL |
Note: Sample data assumes two paid orders for customer 7 and no paid order for Indian customer 8. The qualified labels distinguish the two id columns; an unaliased client may display both with the label id.
Key takeaway: Left-table filters → WHERE; right-table filters → ON.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Name the preserved table
Decide which table's rows must all appear. That's the left table of the LEFT JOIN.
keep every customer
When to use it & trade-offs
What it is
In an outer join, ON decides which rows *match*, while WHERE decides which rows of the *joined result* survive. For a LEFT JOIN, every left row is kept regardless of ON; a condition on the right table in WHERE then removes the rows where that side is NULL, silently turning the LEFT JOIN into an INNER JOIN. For inner joins, ON and WHERE give the same result.
Why it matters: It's the most common outer-join bug in real code, and a favourite interview trick question: two queries that look nearly identical return different row counts. The subtler half, that a *left*-table condition in ON doesn't filter the left table at all, catches even experienced developers.
When to reach for it
- Reports that must show zero values for entities without activity
- Restricting matches to a date range or status
- Debugging "rows disappeared after I added a filter"
When to avoid or be careful
- Right-table conditions in WHERE (except IS NULL anti-joins)
- Left-table conditions in ON when you meant to filter
- INNER JOINs after a LEFT JOIN that silently drop NULL-padded rows
Where a condition goes in a LEFT JOIN
Strengths: Preserves the outer join.
Watch out: Easy to forget when refactoring.
Strengths: Clear way to find rows with no match.
Watch out: Any other condition turns the join into an inner join.
Strengths: Filters the preserved table as intended.
Watch out: None; in ON it would have no filtering effect.
Interview signals & numbers to know
When this topic comes up
- “My LEFT JOIN behaves like an INNER JOIN”
- “Customers with zero orders are missing”
- “ON vs WHERE in outer joins”
- Counts drop after adding a filter
- Multi-table outer joins
Benchmarks & rules of thumb
- All left rows
- kept by LEFT JOIN regardless of ON
- 0
- difference between ON and WHERE for inner joins
- IS NULL
- the one right-table WHERE filter that keeps the anti-join
Common mistakes & follow-ups
Common interview mistakes
- Adding WHERE o.status = 'paid' and losing every customer without a paid order.
- Writing ON … AND c.active = true and expecting inactive customers to disappear.
- Following a LEFT JOIN with an INNER JOIN to a child of the right table, dropping unmatched rows again.
Likely interviewer follow-ups
- Why does a WHERE condition on the right table turn a LEFT JOIN into an INNER JOIN?
- What happens if you put a left-table condition in the ON clause of a LEFT JOIN?
- Are ON and WHERE interchangeable for INNER JOINs?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Filtering in ON Clause vs WHERE Clause in Outer Joins with hands-on SQL practice and interview Q&A.