← Back to SQL, page 3

sql-0051 · Hard senior

In a LEFT JOIN, what happens if you put a condition on the left table in the ON clause, for example ON e.student_id = s.id AND s.city = 'Pune'?

Left rows are never removed by the ON clause of a LEFT JOIN. A false left-side condition only means those rows find no match, so they appear with NULLs on the right. Students outside Pune are still listed, just without enrolments. To restrict the left table, the condition must go in WHERE.

Full answer

This is the mirror image of the more common WHERE-versus-ON bug, and it confuses people who expect the condition to filter.

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);
sql
SELECT s.name, s.city, e.course_id
FROM students s
LEFT JOIN enrolments e
       ON e.student_id = s.id
      AND s.city = 'Pune'
ORDER BY s.name, e.course_id;
namecitycourse_id
AshaPune10
AshaPune20
BenLeedsNULL
ChenPune10
ChenPune20
ChenPune30
DaraNULLNULL
EliLeedsNULL

Every student is still there. Ben, Dara and Eli are not from Pune, so for them the ON condition is false for every enrolment. A LEFT JOIN promises to return each left row at least once, so they come back with NULLs, as if they had no enrolments. That is misleading: Ben and Eli do have enrolments.

If the intent was "Pune students with their enrolments", the city filter belongs in WHERE:

sql
SELECT s.name, s.city, e.course_id
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
WHERE s.city = 'Pune'
ORDER BY s.name, e.course_id;
namecitycourse_id
AshaPune10
AshaPune20
ChenPune10
ChenPune20
ChenPune30

For an INNER JOIN the placement does not change the result, because a failed condition removes the row either way. The difference exists only for outer joins, which is why it is worth checking every outer join for conditions in the wrong clause.

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, s.city, e.course_id
FROM students s
LEFT JOIN enrolments e
       ON e.student_id = s.id
      AND s.city = 'Pune'
ORDER BY s.name, e.course_id;

SELECT s.name, s.city, e.course_id
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
WHERE s.city = 'Pune'
ORDER BY s.name, e.course_id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • ON in a LEFT JOIN controls matching, never removal of left rows
  • A false left-side condition in ON produces NULL-filled rows
  • Filters on the preserved table belong in WHERE
  • For INNER JOIN, ON and WHERE placement give the same result

Common mistakes

  • Putting a left-table filter in ON and getting rows that look like 'no enrolments'
  • Assuming ON and WHERE are interchangeable for outer joins

Follow-ups

  • Write the query for 'all students, with only their Pune-campus enrolments'.
  • Does the optimizer treat ON and WHERE differently for inner joins?

Tips

  • Point at Ben: he has enrolments, but the result shows none