WHERE vs ON clause filtering

Concepts covered: Execution Order

A join query has 2 places to put a condition. ON is consulted while rows are being paired: it decides which pairs are accepted. WHERE is consulted after the join has finished: it decides which finished rows stay. Under INNER JOIN the 2 places give the same result, because a pair rejected early and a row removed late are both gone. Under LEFT JOIN they differ, and the reason is the padding step that sits between them.

Take one request: list every customer, and show their orders over 60 if they have any. The first attempt puts the amount test in WHERE.

SELECT
  c.name,
  o.total
FROM customers c
LEFT JOIN orders o
  ON c.cust_id = o.cust_id
WHERE o.total > 60
customers
cust_idname
C1Alice
C2Bob
C3Carol
C4Dave
orders
order_idcust_idtotal
O1C150
O2C130
O3C375

The join builds the familiar 5 rows, then WHERE o.total > 60 tests each one. Alice's rows hold 50 and 30, so both are false. Carol's holds 75, so it is true. The rows for Bob and Dave hold NULL, and NULL > 60 evaluates to NULL, which WHERE treats the same as false. One row is left: Carol, 75. The request said every customer, and 3 of the 4 are gone. A WHERE test on a right-table column has turned the LEFT JOIN back into an INNER JOIN.

Move the test into ON

The fix moves the amount test up into the ON clause, joined to the key match with AND.

SELECT
  c.name,
  o.total
FROM customers c
LEFT JOIN orders o
  ON c.cust_id = o.cust_id
  AND o.total > 60
customers
cust_idname
C1Alice
C2Bob
C3Carol
C4Dave
orders
order_idcust_idtotal
O1C150
O2C130
O3C375

Now a pair is accepted only if the ids match and the total is over 60. Alice's 2 orders match her id and fail the amount test, so Alice has no accepted pair. Padding treats her the same as Bob and Dave: one row, NULL on the right. Carol's order passes both tests. The result keeps one row for each of the 4 customers, and only Carol's shows a total. The condition narrowed which orders may attach, and it could not remove a customer.

Condition is aboutWritten in ONWritten in WHERE
The right table (orders)Limits which orders attach. Every customer stays.Removes padded rows. Acts like INNER JOIN.
The left table (customers)Limits which customers get orders attached. Every customer stays.Removes those customers. Safe and expected.

In the store's tables, Alice has orders of 50 and 30 and Carol has one of 75, while Bob and Dave have none. What does this query return?

SELECT
  c.name,
  o.total
FROM customers c
LEFT JOIN orders o
  ON c.cust_id = o.cust_id
  AND o.total > 40

A left-table condition in ON

A condition on the left table behaves in a way that surprises people when it is written in ON. Suppose you want to leave Alice out and write AND c.name != 'Alice' in the ON clause. Alice still appears. ON can only reject pairs, so her 2 orders fail to attach and padding returns her with NULLs. To remove left rows, the condition belongs in WHERE.

SELECT
  c.name,
  o.order_id
FROM customers c
LEFT JOIN orders o
  ON c.cust_id = o.cust_id
  AND c.name != 'Alice'
ORDER BY c.name
customers
cust_idname
C1Alice
C2Bob
C3Carol
C4Dave
orders
order_idcust_idtotal
O1C150
O2C130
O3C375
Result
nameorder_id
AliceNULL
BobNULL
CarolO3
DaveNULL

The OR IS NULL patch

A common repair keeps the test in WHERE and adds an escape hatch: WHERE o.total > 60 OR o.total IS NULL. It rescues Bob and Dave, whose padded rows now pass. It still loses Alice. Her rows are real orders with totals of 50 and 30, so they fail both halves of the test. The patch returns 3 customers where the ON version returns 4. It answers "customers with a big order or no orders", which is a different question.

Do
  • Put right-table filters in ON when every left row must stay
  • Put left-table filters in WHERE to remove left rows
  • Keep IS NULL anti-join tests in WHERE, where padded rows exist
Avoid
  • Filter a right-table column in WHERE and expect unmatched rows to survive
  • Patch a WHERE filter with OR IS NULL and call it equivalent

Marketing wants every customer listed once per order of 50 or more. A customer with no such order must still appear, with NULL for the total. Arrange the pieces into that query. 2 tiles do not belong.

For each condition, ask: when no right row passes it for some left row, should that left row disappear or stay with NULLs beside it? Disappear means WHERE. Stay means ON. Ask it for each condition separately, because one query often needs both answers.

About this lesson

This section is part of the Joins: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.