Sub topic 7
Filtering Joined Rows: `WHERE` Versus `ON`
Prerequisites and the two jobs
This chapter assumes that you know basic SELECT, FROM, WHERE, and ORDER BY syntax, and that you understand primary keys and foreign keys. We use the customers and orders tables from the earlier examples.
A join condition and a filter answer different questions:
ONsays which rows from two tables are allowed to match.WHEREremoves rows from the joined result.
For an INNER JOIN, many conditions produce the same final rows whether they appear in ON or WHERE. Still, placing each condition in the right location makes the query easier to understand.
Conditions in `ON`
Use ON primarily for the relationship between tables. In this query, the condition identifies the customer who owns each order:
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;You can add an order condition to 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;For an inner join, this returns the same rows as placing o.total > 30 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;Both queries return orders 101 and 102 for Ada. Ben's order 103 is below 30, so it is removed. Chloe has no matching order, so she does not appear.
The difference becomes important with outer joins. For example, with a LEFT JOIN, a condition in ON can preserve customers who have no qualifying order:
SELECT c.name, o.id AS order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
AND o.total > 30;Chloe remains in the result with a NULL order ID. If o.total > 30 moved to WHERE, Chloe would be removed because NULL > 30 is unknown. Outer joins are covered in more detail later.
A practical placement rule
Keep the relationship in ON and put result filters in WHERE:
| Query part | Typical purpose | Example |
|---|---|---|
| `ON` | Connect related rows | `o.customer_id = c.id` |
| `WHERE` | Filter the joined result | `o.total > 30` |
This rule is not a performance requirement. Database optimizers can often move safe predicates internally. It is a readability rule that helps readers see the table relationship immediately.
Debugging filters
When a join returns too few rows, check these questions:
- Does every row satisfy the
ONcondition? - Does a
WHEREcondition reject rows after they match? - Can either join column contain
NULL? - Are you filtering a one-to-many table and unintentionally changing the row count?
Start by running the join without the filter. Then add the WHERE condition and compare the results.
Exercise
Where should o.customer_id = c.id normally go, and where should o.total > 30 normally go?
Show answer
Put the relationship o.customer_id = c.id in ON, and put the result filter o.total > 30 in WHERE. With an INNER JOIN, both placements often return the same rows, but this arrangement is clearer and remains safer when the join changes to an outer join.