SteelReed

Search

Search posts, questions…

SQL & Query TuningQuestion 10 / 37

Middle

Does it matter whether a condition goes in ON or WHERE, and why does a WHERE filter break a LEFT JOIN?

Short answer

With INNER JOIN, a condition in ON or WHERE gives the same result. With LEFT JOIN it's completely different: WHERE runs after the join and drops rows whose right side is NULL.

Join + where the right-table condition goes Unmatched left row
INNER JOIN, in ON or WHERE Dropped (same either way)
LEFT JOIN, in ON Kept, right columns = NULL
LEFT JOIN, in WHERE Dropped: becomes an INNER JOIN

3 points to say:

  1. INNER: ON or WHERE is the same.
  2. LEFT: filtering the right table in WHERE makes it inner.
  3. Right table → ON, left table → WHERE.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.