← Back to SQL, page 3

sql-0050 · Medium mid

A LEFT JOIN report of every student and their Databases mark drops students who never took Databases. The filter is in the WHERE clause. Why, and how do you fix it?

WHERE runs after the join. For students without a Databases enrolment, the right-side columns are NULL, so a WHERE condition on them is UNKNOWN and the rows are removed, which turns the LEFT JOIN into an inner join. Move the condition on the right table into the ON clause so it limits which rows can match, not which left rows survive.

Full answer

The goal is a list of all students with their Databases mark, blank if they never took it.

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

The first attempt puts the course filter in WHERE:

sql
SELECT s.name, e.marks AS databases_mark
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
WHERE e.course_id = 10
ORDER BY s.name;
namedatabases_mark
Asha88
Ben75
Chen64

Dara and Eli are missing. Here is what happened, step by step:

  1. The LEFT JOIN produced every enrolment for every student, plus a NULL row for Dara.
  2. WHERE then kept only rows with course_id = 10. Eli's only row has course_id = 20, and Dara's has NULL. Both fail, so both students vanish.

The fix is to make the course part of the matching rule:

sql
SELECT s.name, e.marks AS databases_mark
FROM students s
LEFT JOIN enrolments e
       ON e.student_id = s.id
      AND e.course_id  = 10
ORDER BY s.name;
namedatabases_mark
Asha88
Ben75
Chen64
DaraNULL
EliNULL

Now the join looks only for Databases enrolments. Students without one still appear, with NULL marks.

The rule to remember: in a LEFT JOIN, conditions on the right table belong in ON if you want to keep every left row. Conditions on the left table usually belong in WHERE. A condition such as WHERE e.course_id IS NULL is the exception that is meant to be in WHERE, because it deliberately keeps only the unmatched rows.

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

SELECT s.name, e.marks AS databases_mark
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
WHERE e.course_id = 10
ORDER BY s.name;

SELECT s.name, e.marks AS databases_mark
FROM students s
LEFT JOIN enrolments e
       ON e.student_id = s.id
      AND e.course_id  = 10
ORDER BY s.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • WHERE is applied after the join
  • A WHERE condition on right-side columns removes the NULL rows a LEFT JOIN added
  • Put right-table filters in ON to keep all left rows
  • WHERE right.key IS NULL is the intended anti-join exception

Common mistakes

  • Filtering the right table in WHERE and silently getting an inner join
  • Moving left-table filters into ON, which does not remove left rows

Follow-ups

  • What happens if you put a condition on the left table into the ON clause of a LEFT JOIN?
  • How would you list students who did not take Databases?

Tips

  • Walk through the rows before and after WHERE; it makes the cause obvious