ON versus WHERE: preserve outer-join rows
Overview
ON decides which input pairs match; WHERE filters the result of the join. For a LEFT JOIN, putting a right-side condition in ON can retain unmatched left rows. Putting a null-rejecting condition in WHERE removes those rows.
First example
SELECT c.id, o.idFROM customers AS c LEFT JOIN orders AS o ON c.id = o.customer_id AND o.id >= 102ORDER BY c.id, o.id;Observed SQLite result (rows in JSON):
[ [ 1, 102 ], [ 2, 103 ], [ 3, null ]]How it works
In the first query the requirement is to show every customer with orders numbered 102 or above. Alice has 102, David has 103, Sara remains with NULL. In the second query the WHERE predicate rejects Sara because comparing NULL to 102 is UNKNOWN. A customer who has orders but none meeting the condition also disappears in the second form. Adding OR o.id IS NULL to WHERE does not recover such customers, because their existing matches prevented NULL extension. For ordinary inner joins, equivalent deterministic predicates can usually be moved between ON and WHERE; outer joins are different. This describes logical results, not physical execution order.
Practical example
SELECT c.id, o.idFROM customers AS c LEFT JOIN orders AS o ON c.id = o.customer_idWHERE o.id >= 102ORDER BY c.id, o.id;Observed SQLite result (rows in JSON):
[ [ 1, 102 ], [ 2, 103 ]]Practice and mistakes
Practice: change the threshold to 103. Predict the ON version: Alice and Sara have NULL, David has 103. Predict the WHERE version: only David. Decide whether the requirement is to preserve all customers or only qualifying matches.