SQL compared
ON vs WHERE in a LEFT JOIN
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
In a LEFT JOIN, a condition in ON decides which right-hand rows match, and every left row stays. The same condition in WHERE runs after the join and removes the left rows that had no match, which silently turns the LEFT JOIN into an inner join.
Side by side
| Condition in ON | Condition in WHERE | |
|---|---|---|
| Runs | While matching rows | After the join |
| Left rows without a match | Kept, with NULLs | Removed |
| Result | Still a LEFT JOIN | Behaves like an INNER JOIN |
| Use it for | Filters on the right table | Filters on the left table |
The same data, each way
Every query below was run on PostgreSQL and SQLite when this page was built; the tables are what came back.
Filter in ON: every customer stays
Each customer with their delivered orders; customers with none appear once with a NULL order.
SELECT c.name, o.id AS delivered_order
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.id AND o.status = 'delivered'
ORDER BY c.name, o.id;Result (PostgreSQL and SQLite, identical): first 12 of 16 rows
| name | delivered_order |
|---|---|
| Diego Santos | NULL |
| Ellie Park | 6 |
| Ellie Park | 14 |
| Jonah Reed | 2 |
| Jonah Reed | 7 |
| Jonah Reed | 13 |
| Lena Fischer | 10 |
| Marcus Bell | 12 |
| Maya Ortiz | 1 |
| Maya Ortiz | 3 |
| Maya Ortiz | 9 |
| Nora Lindqvist | NULL |
Try it: PostgreSQL playground · SQLite playground
Filter in WHERE: customers vanish
Same filter after the join: the NULL rows fail o.status = 'delivered', so customers without a delivered order disappear.
SELECT c.name, o.id AS delivered_order
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'delivered'
ORDER BY c.name, o.id;Result (PostgreSQL and SQLite, identical): first 12 of 13 rows
| name | delivered_order |
|---|---|
| Ellie Park | 6 |
| Ellie Park | 14 |
| Jonah Reed | 2 |
| Jonah Reed | 7 |
| Jonah Reed | 13 |
| Lena Fischer | 10 |
| Marcus Bell | 12 |
| Maya Ortiz | 1 |
| Maya Ortiz | 3 |
| Maya Ortiz | 9 |
| Sam Okafor | 8 |
| Sam Okafor | 15 |
Try it: PostgreSQL playground · SQLite playground
The difference in numbers
SELECT
(SELECT COUNT(DISTINCT c.id) FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'delivered') AS customers_with_on,
(SELECT COUNT(DISTINCT c.id) FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'delivered') AS customers_with_where;Result (PostgreSQL and SQLite, identical): 1 row
| customers_with_on | customers_with_where |
|---|---|
| 10 | 7 |
Try it: PostgreSQL playground · SQLite playground
When to use which
- Condition in ON
Conditions on the right-hand table when you want to keep every left row: “each customer and their delivered orders, if any”.
- Condition in WHERE
Conditions on the left-hand table (
WHERE c.city = 'Austin'), orWHERE o.id IS NULLto find rows with no match.
Common mistakes
Filtering the right table in WHERE and wondering why the LEFT JOIN lost rows.
Moving a left-table filter into ON: it doesn't remove left rows, it only stops them matching. Keep those in WHERE.
Counting with
COUNT(*)after the LEFT JOIN: unmatched rows count as 1. Count a right-hand column.
PostgreSQL and SQLite
Everything on this page works the same in PostgreSQL and SQLite: each example above was run on both, and the results match.
Official documentation
Go deeper
- Guide: SQL JOINs explained
- Practice: SQL JOIN practice: 6 exercises with answers
- Lesson: LEFT JOIN (Pro)
Make it stick in five minutes a day
“LEFT JOIN” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.