ON vs WHERE in Joins
When a predicate belongs in ON versus WHERE and why it matters for results.
A Predicate Can Live in Two Places
Once you can write an INNER JOIN, the next interview question is sharper: does this condition belong in ON or in WHERE?
For an INNER JOIN the answer is often "it does not matter for the result." But the moment you switch to an outer join, the choice changes the answer completely. Interviewers ask this precisely because juniors put everything in WHERE out of habit.
This lesson makes the rule crisp.
What ON Does
The ON clause defines how rows are paired. It runs as the join is built, deciding which row from the left table matches which row from the right.
Think of ON as answering the question: "for these two rows, do they belong together?"
SELECT c.name, o.amount
FROM customers c
JOIN orders o
ON o.customer_id = c.id; -- pairing rule