sqlstreak

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 ONCondition in WHERE
RunsWhile matching rowsAfter the join
Left rows without a matchKept, with NULLsRemoved
ResultStill a LEFT JOINBehaves like an INNER JOIN
Use it forFilters on the right tableFilters 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

namedelivered_order
Diego SantosNULL
Ellie Park6
Ellie Park14
Jonah Reed2
Jonah Reed7
Jonah Reed13
Lena Fischer10
Marcus Bell12
Maya Ortiz1
Maya Ortiz3
Maya Ortiz9
Nora LindqvistNULL

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

namedelivered_order
Ellie Park6
Ellie Park14
Jonah Reed2
Jonah Reed7
Jonah Reed13
Lena Fischer10
Marcus Bell12
Maya Ortiz1
Maya Ortiz3
Maya Ortiz9
Sam Okafor8
Sam Okafor15

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_oncustomers_with_where
107

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'), or WHERE o.id IS NULL to 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

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.

Start free