Quick revision
The short answer
For an inner join there is no difference: a condition in ON and the same condition in WHERE give identical results, and the optimizer treats them alike. For a LEFT JOIN there is a big difference: a condition on the right table in ON only decides which rows match, so unmatched left rows are kept with NULLs, while the same condition in WHERE removes those NULL rows and turns the join into an inner join. So customers LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'PAID' lists every customer, with their paid orders or NULL.
What to remember
In this answer, we saw that ON decides which rows pair up and WHERE filters the joined result. For inner joins the two placements are equivalent and the optimizer treats them the same. For a LEFT JOIN, a condition on the right table in ON keeps unmatched left rows with NULLs, while in WHERE it removes them and silently produces an inner join. Conditions on the preserved table belong in WHERE, conditions on the optional table in ON, and code review should flag WHERE tests on the nullable side.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For an inner join there is no difference: a condition in ON and the same condition in WHERE give identical results, and the optimizer treats them alike. For a LEFT JOIN there is a big difference: a condition on the right table in ON only decides which rows match, so unmatched left rows are kept with NULLs, while the same condition in WHERE removes those NULL rows and turns the join into an inner join. So customers LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'PAID' lists every customer, with their paid orders or NULL.
Why this question is asked
Where to put a condition looks like a style question, and for inner joins it is one. For outer joins it changes the result, and the mistake does not raise an error, it just makes rows disappear. Interviewers ask the two halves together because the contrast shows whether we understand what ON and WHERE each do, rather than following a habit. In this answer, we will recap the roles of ON and WHERE, show that they are interchangeable for an inner join, show why they are not for a left join using orders and a status filter, and finish with a placement convention that makes intent clear in code review.
1A Quick Recap of ON and WHERE
The ON clause belongs to a join. It is the condition that decides whether a row from the left table and a row from the right table form a pair. The WHERE clause belongs to the whole query. It is applied after all the joins in FROM have produced their rows, and it keeps only rows for which the condition is true.
For an inner join, rows without a partner are simply not produced, so whether a condition rejects a pair while matching or rejects the joined row afterwards makes no difference. For an outer join, the join adds something extra: rows from the preserved side that found no partner, padded with NULLs. That extra step is where ON and WHERE diverge.
2Inner Join: ON and WHERE Are Equivalent
Here are two ways to list customers with their paid orders.
Both give the same rows, and EXPLAIN shows the same plan: the optimizer moves conditions freely between ON and WHERE for inner joins, because the result is the same either way. Logically, an inner join is a filtered cross product, and filtering in one step or in two steps gives the same set.
3Left Join: Where the Condition Goes Changes the Result
Now we want every customer, with their paid orders where they exist. Ana has paid orders 101 and 104, Ben has only a cancelled order 102, and Cem has no orders.
| Customer | Condition in ON | Condition in WHERE |
|---|---|---|
| Ana (paid 101, 104) | Ana 101, Ana 104 | Ana 101, Ana 104 |
| Ben (cancelled 102) | Ben NULL | removed: 'CANCELLED' <> 'PAID' |
| Cem (no orders) | Cem NULL | removed: NULL = 'PAID' is unknown |
In the first query, the status test is part of matching. Ben's cancelled order fails it, so Ben has no matching order, and the LEFT JOIN keeps him with NULLs, exactly like Cem. In the second query, the join first produces Ben with his cancelled order and Cem with NULLs. Then WHERE runs: for Ben the status is 'CANCELLED', which is not 'PAID', so the row goes; for Cem the status is NULL, and NULL = 'PAID' is unknown, so that row goes too. The second query has become an inner join in everything but name, and PostgreSQL's planner will even plan it as one.
4Which Side the Condition Is On Matters
The difference only arises for conditions on the optional table, the right side of a LEFT JOIN. A condition on the preserved table behaves differently. If we write LEFT JOIN orders o ON o.customer_id = c.id AND c.city = 'Lisbon', customers outside Lisbon are not removed; they just never match any order and appear with NULLs. That surprises people, because the intention was almost certainly to list only Lisbon customers, which belongs in WHERE.
So the rule is a pair. Conditions on the preserved table go in WHERE, because they decide which rows we want. Conditions on the optional table go in ON, because they decide what counts as a match. The one deliberate exception is testing the optional side for NULL in WHERE, as in WHERE o.id IS NULL, which is how we write an anti-join: customers with no paid order.
5A Convention for Readable Joins
Many teams adopt a simple convention that makes intent visible. ON contains the join keys, plus filters on the optional side of an outer join. WHERE contains filters on the preserved side, and, for inner joins, row filters on any table. Following it, a reader can tell at a glance what a query is meant to return.
In code review, one pattern deserves a comment every time: a LEFT JOIN whose WHERE clause tests a column of the right-hand table with anything other than IS NULL. Either the join should be an inner join, which says honestly what the query does, or the condition belongs in ON. Some SQL linters flag this pattern automatically, and an EXPLAIN that shows an inner join for a query written with LEFT JOIN confirms it.
6Follow-up questions
What about a FULL OUTER JOIN?
Both sides are preserved, so a WHERE condition on either side can remove unmatched rows from the other. Filters for a full join usually go into subqueries that restrict each input before the join, rather than into ON or WHERE.
Does the condition placement affect performance for inner joins?
No. The optimizer normalises them and pushes filters down to where they are cheapest, so the plan is the same. Placement for inner joins is about readability only.
How do I count paid orders per customer including zero?
Keep the status condition in ON, group by the customer, and count a column of orders, COUNT(o.id), which ignores the NULL-padded rows. COUNT(*) would give 1 for Ben and Cem.
7Conclusion
In this answer, we saw that ON decides which rows pair up and WHERE filters the joined result. For inner joins the two placements are equivalent and the optimizer treats them the same. For a LEFT JOIN, a condition on the right table in ON keeps unmatched left rows with NULLs, while in WHERE it removes them and silently produces an inner join. Conditions on the preserved table belong in WHERE, conditions on the optional table in ON, and code review should flag WHERE tests on the nullable side.
Open with
For an inner join there is no difference in the result. ON decides which pairs of rows match and WHERE filters the joined rows afterwards, but since an inner join never produces unmatched rows, filtering during matching or after it gives the same set, and the optimizer moves those conditions around freely, so the plan is the same too.
Then, if they want more
For an outer join it matters a lot. Take customers LEFT JOIN orders and a filter status equals 'PAID'. If I put the status in the ON clause, it becomes part of the match: a customer with only cancelled orders, or no orders, has no matching paid order, so the LEFT JOIN keeps them with NULLs.
That gives every customer with their paid orders or NULL. If I put the same condition in WHERE, the join first produces the cancelled orders and the NULL rows, and then WHERE removes them, because 'CANCELLED' is not 'PAID' and NULL compared to 'PAID' is unknown. The query silently becomes an inner join.
The rule I follow is that conditions on the preserved table, the customers, go in WHERE, and conditions on the optional table go in ON, except IS NULL checks for anti-joins. A condition on the preserved table in ON is the reverse trap: it does not filter customers, it only stops them matching.
In code review I flag any WHERE condition on the nullable side of an outer join.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Is there a difference between putting a condition in the ON clause and in the WHERE...": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/ce2a464f49/