Full answer
"Students who took every 4-credit course" is relational division: divide the enrolment relation by the set of required courses.
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 4-credit courses are Databases (10) and Algorithms (20).
The counting method compares each student's number of required courses with the size of the set:
SELECT s.name
FROM students s
JOIN enrolments e ON e.student_id = s.id
JOIN courses c ON c.id = e.course_id AND c.credits = 4
GROUP BY s.id, s.name
HAVING COUNT(DISTINCT c.id) = (SELECT COUNT(*) FROM courses WHERE credits = 4)
ORDER BY s.name;| name |
|---|
| Asha |
| Chen |
Asha and Chen have both. Ben took Databases only, Eli took Algorithms only.
The double NOT EXISTS method reads like the definition: "there is no required course that this student has not taken".
SELECT s.name
FROM students s
WHERE NOT EXISTS (
SELECT 1 FROM courses c
WHERE c.credits = 4
AND NOT EXISTS (
SELECT 1 FROM enrolments e
WHERE e.student_id = s.id AND e.course_id = c.id))
ORDER BY s.name;| name |
|---|
| Asha |
| Chen |
Both give the same answer. The counting version is usually easier to read and adapt. The double negative is worth knowing because it handles one edge case more naturally: if the required set is empty, NOT EXISTS returns every student (every student has taken "all" of zero courses), while the counting version above returns none, because the inner join leaves no rows to group. Decide which behaviour is right for the business and test it:
SELECT COUNT(*) AS students_returned_for_empty_set
FROM students s
WHERE NOT EXISTS (
SELECT 1 FROM courses c
WHERE c.credits = 99
AND NOT EXISTS (
SELECT 1 FROM enrolments e
WHERE e.student_id = s.id AND e.course_id = c.id));| students_returned_for_empty_set |
|---|
| 5 |
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
FROM students s
JOIN enrolments e ON e.student_id = s.id
JOIN courses c ON c.id = e.course_id AND c.credits = 4
GROUP BY s.id, s.name
HAVING COUNT(DISTINCT c.id) = (SELECT COUNT(*) FROM courses WHERE credits = 4)
ORDER BY s.name;
SELECT s.name
FROM students s
WHERE NOT EXISTS (
SELECT 1 FROM courses c
WHERE c.credits = 4
AND NOT EXISTS (
SELECT 1 FROM enrolments e
WHERE e.student_id = s.id AND e.course_id = c.id))
ORDER BY s.name;
SELECT COUNT(*) AS students_returned_for_empty_set
FROM students s
WHERE NOT EXISTS (
SELECT 1 FROM courses c
WHERE c.credits = 99
AND NOT EXISTS (
SELECT 1 FROM enrolments e
WHERE e.student_id = s.id AND e.course_id = c.id));Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Relational division: related to every member of a set
- Counting: COUNT(DISTINCT required) = size of the set
- Double NOT EXISTS: no required item is missing
- The two forms differ when the required set is empty
Common mistakes
- Using IN, which returns students with any required course, not all
- Comparing with a hard-coded count that goes stale when courses change
Follow-ups
- How would you find students who took exactly the required courses and nothing else?
- Which form performs better on a large table, and how would you check?
Tips
- Name the pattern; interviewers often ask for 'relational division' by name