Session Notes: Why Did My LEFT JOIN Lose Rows?

These are written notes in the style of a short classroom talk. There is no event, date or recording; the format just keeps the ideas in order. The topic is one of the most common beginner puzzles with joins.

Writing your first SQL queries illustration

Agenda

  1. A quick reminder of what LEFT JOIN promises
  2. The surprise
  3. Why it happens
  4. Two fixes
  5. Questions from learners

1. What LEFT JOIN promises

A LEFT JOIN keeps every row from the left table, even when there is no match on the right. Where nothing matches, the right-hand columns are filled with NULL, meaning "nothing here".

SELECT students.name, clubs.club_name
FROM students
LEFT JOIN clubs ON clubs.student_id = students.id;

Every student appears, including those in no club.

2. The surprise

Now we only want to see chess club memberships, but still list every student:

SELECT students.name, clubs.club_name
FROM students
LEFT JOIN clubs ON clubs.student_id = students.id
WHERE clubs.club_name = 'Chess';

Students who are not in the chess club vanish entirely. It looks as if LEFT JOIN broke its promise.

3. Why it happens

Remember the order of work: the join happens first, then WHERE filters the joined rows. For a student in no club, club_name is NULL. The test NULL = 'Chess' is not true, so WHERE throws that row away. The LEFT JOIN did keep the row; the WHERE removed it afterwards.

4. Two fixes

Fix A: move the condition into ON.

SELECT students.name, clubs.club_name
FROM students
LEFT JOIN clubs ON clubs.student_id = students.id AND clubs.club_name = 'Chess';

Now the condition decides which club rows count as a match. Every student stays; only chess memberships fill in the right side.

Fix B: allow the empty rows in WHERE.

WHERE clubs.club_name = 'Chess' OR clubs.club_name IS NULL

This keeps students with no clubs at all, but drops students who are only in other clubs, which is usually not what you want. Fix A is normally the right answer.

Key points to remember

Where the condition goesEffect on a LEFT JOIN
In ONDecides what counts as a match; all left rows stay
In WHERE on a right-side columnRemoves unmatched rows, behaving like an INNER JOIN
In WHERE on a left-side columnFilters left rows as usual; safe

5. Questions from learners

"So should I never use WHERE with LEFT JOIN?" Use it freely for columns of the left table. Be careful only with conditions on the right table.

"How do I find students in no club at all?" WHERE clubs.student_id IS NULL after the LEFT JOIN. Here the NULL is exactly what you are looking for.

"Is RIGHT JOIN the opposite?" Yes, it keeps every row from the right table. Most people just swap the table order and use LEFT JOIN, which is easier to read.

"Why do some students appear twice?" A student in two clubs matches two rows. That is correct; group or filter if you want one line each.

For more practice, work through INNER JOIN and LEFT JOIN, one small step at a time.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

Illustrative outline only: no live session, recording, registration or certificate is offered on this site. Any speakers, panels or schedules mentioned are hypothetical.

← Back to Home | View More Webinars