Full answer
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 query as written:
sql
SELECT s.name, e.marks FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
WHERE e.course_id = 30
ORDER BY s.id;| name | marks |
|---|---|
| Ben | NULL |
| Chen | 80 |
Students without a Statistics enrolment get NULL in every enrolment column. WHERE e.course_id = 30 is then NULL for them, so they are dropped. Only Ben and Chen remain, and Ben's NULL mark only appears because his enrolment row exists with no mark.
A corrected version:
sql
SELECT s.name, e.marks FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id AND e.course_id = 30
ORDER BY s.id;| name | marks |
|---|---|
| Asha | NULL |
| Ben | NULL |
| Chen | 80 |
| Dara | NULL |
| Eli | NULL |
With the condition in ON, every student is kept and only the Statistics enrolment is attached when it exists. The output now has five rows. Note that Ben and Asha both show NULL for different reasons: Ben has an unmarked enrolment, Asha has none. Add e.course_id to the select list when that distinction matters.
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 FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
WHERE e.course_id = 30
ORDER BY s.id;
SELECT s.name, e.marks FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id AND e.course_id = 30
ORDER BY s.id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- WHERE on the right table can cancel a LEFT JOIN
- Put right-table filters in ON
- NULL marks can mean no row or an unmarked row
Common mistakes
- Adding OR e.course_id IS NULL, which also keeps students with other courses wrongly
- Switching to INNER JOIN and losing students
Follow-ups
- How would you tell 'not enrolled' from 'enrolled, no mark'?
- Does the same rule apply to RIGHT and FULL joins?
Tips
- Say which rows the WHERE removed