Sub topic 7
Filtering Joined Rows: WHERE versus ON
This chapter assumes you can write a basic INNER JOIN ... ON query and use WHERE and ORDER BY. It also assumes you know that a join condition decides which rows pair up. The examples use the customers and orders tables from the sample schema earlier in this tutorial.
Two places to put a condition
A join query has two places that can hold a filter:
- The
ONclause, which decides which rows from the two tables pair up. - The
WHEREclause, which removes rows from the result after the pairing.
For an inner join, a condition in either place gives the same rows. Here is the filter in WHERE:
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id
WHERE o.total > 30
ORDER BY o.id;Here is the same filter moved into ON:
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id
AND o.total > 30
ORDER BY o.id;Both queries return two rows: Ada with order 101 (50.00) and Ada with order 102 (75.00). Ben's order 103 has a total of 20.00, so it fails the filter. Chloe has no orders, and order 104 has no customer. Neither of those two ever pairs up.
Why the results match
The database treats an inner join as a filter over pairs of rows. Conceptually, it forms every combination of rows and keeps the pairs for which all conditions are true. ON and WHERE both express "keep the pair only if this is true", so for an inner join they are interchangeable logically.
Real engines do not build every combination. The optimizer is free to rearrange the conditions. It normally treats the two queries above identically, so you should not choose between them for speed.
The diagram shows the logical order in which a query is evaluated. The ON condition applies while the tables are being joined. WHERE applies to the rows that come out of the join.
Where the difference appears: outer joins
The two placements stop being equivalent once you use an outer join. A LEFT JOIN keeps every row from the left table, even when nothing on the right matches. It fills the right-side columns with NULL for those rows. The next-to-last chapters cover outer joins in full. Here you only need them to see the effect.
Put the filter in ON:
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
AND o.total > 30
ORDER BY c.id, o.id;The result has four rows:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 75.00 |
| Ben | NULL | NULL |
| Chloe | NULL | NULL |
Ben stays in the result. His only order failed the ON condition, so he has no matching order, and a left join keeps him anyway with NULL columns.
Now put the same filter in WHERE:
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
WHERE o.total > 30
ORDER BY c.id, o.id;The result has only Ada's two rows. The join first produces the same four rows as before, plus Ben's order 103 paired with him and Chloe with NULL. Then WHERE o.total > 30 runs. For Chloe, o.total is NULL, and NULL > 30 is not true, so her row is removed. Ben's row with total 20.00 is removed because 20 is not above 30.
A rule of thumb
Use these questions to decide where a condition goes:
- Does the condition say how the two tables relate, such as
o.customer_id = c.id? Put it inON. - Does the condition describe which results you want to see, such as "orders over 30" or "orders from 2026"? Put it in
WHERE. - Are you using an outer join and the condition restricts the optional table, while you still want unmatched rows from the other table? Put it in
ON.
For inner joins, many teams keep ON for relationships and WHERE for filters. The query then reads the same way your question does. It also means that changing INNER JOIN to LEFT JOIN later does not silently change which rows you keep, as long as you decide the placement of each filter deliberately.
Conditions in ON that only mention one table
ON can contain conditions that refer to just one of the joined tables. This is legal, and for inner joins it behaves like a WHERE filter:
SELECT c.name, o.id AS order_id, o.order_date
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id
AND c.name = 'Ada'
AND o.order_date >= DATE '2026-02-01';This returns one row: Ada with order 102, dated 2026-02-10. Writing DATE '...' is standard SQL for a date literal. A few engines accept a bare string instead, but the explicit form is safer.
The risk is readability. A reader looking for the join relationship has to sort it from the filters. With three or four tables this gets confusing quickly.
Filtering on aggregates: WHERE versus HAVING
WHERE runs before grouping, so it cannot refer to aggregate results such as SUM(o.total). To filter groups, use HAVING:
SELECT c.name, COUNT(*) AS order_count, SUM(o.total) AS revenue
FROM customers AS c
INNER JOIN orders AS o ON o.customer_id = c.id
WHERE o.order_date >= DATE '2026-01-01'
GROUP BY c.id, c.name
HAVING SUM(o.total) > 60
ORDER BY c.name;The steps run in order. The join pairs customers with orders, and WHERE keeps orders from 2026 onward. GROUP BY forms one group per customer, and HAVING keeps groups whose revenue exceeds 60. Ada has revenue 125.00 and stays. Ben has 20.00 and is dropped. Use WHERE for conditions on individual rows and HAVING for conditions on group results. Filtering rows in WHERE is also usually cheaper, because fewer rows reach the grouping step.
Common mistakes
- Moving a filter between ON and WHERE in an outer join without checking the result. The row count changes, as shown above. Always compare the output on a small data set.
- Writing a filter on the right table in WHERE after a LEFT JOIN, then wondering why unmatched rows vanished. The
NULLcomparison is not true, so the row is dropped. If you really want to test for missing matches, writeWHERE o.id IS NULLinstead. - Putting the whole join relationship in WHERE. With
JOINwritten withoutON, or with commas, a forgotten condition produces every combination of rows. Keep join conditions inONso the database rejects a missing one. - Forgetting that ON can refer only to tables already in the join. In most engines, an
ONclause cannot name a table that appears later in theFROMlist. Order your joins so eachONrefers to tables already introduced.
Exercise
Using the sample data, you run SELECT c.name, o.id FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.id WHERE o.total > 30;. Chloe does not appear in the result. Why not, and how would you change the query so Chloe appears with NULL in the order column while still only matching orders over 30?
Show answer
Chloe has no orders, so the left join gives her a row where o.total is NULL. The WHERE condition NULL > 30 is not true, so the row is removed. Move the filter into ON: LEFT JOIN orders AS o ON o.customer_id = c.id AND o.total > 30 and remove the WHERE clause. Chloe then appears with NULL in o.id, and Ben appears too, because his only order fails the ON condition.
Exercise
For an INNER JOIN between customers and orders, you write the condition o.total > 30 once in ON and once in WHERE. Do the two queries return different rows? Why or why not?
Show answer
No, they return the same rows (Ada's orders 101 and 102). In an inner join, a pair of rows is kept only if every condition is true, whether the condition is in ON or in WHERE. The difference between the two clauses only matters for outer joins, where ON decides which rows match and WHERE removes rows afterwards.