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.
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;| name | city | course_id |
|---|---|---|
| Asha | Pune | 10 |
| Asha | Pune | 20 |
| Ben | Leeds | NULL |
| Chen | Pune | 10 |
| Chen | Pune | 20 |
| Chen | Pune | 30 |
| Dara | NULL | NULL |
| Eli | Leeds | NULL |
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:
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;| name | city | course_id |
|---|---|---|
| Asha | Pune | 10 |
| Asha | Pune | 20 |
| Chen | Pune | 10 |
| Chen | Pune | 20 |
| Chen | Pune | 30 |
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.
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