← Back to SQL, page 6

sql-0102 · Medium mid

Write a query for students who have not taken any 4-credit course.

Use NOT EXISTS with a correlated subquery that joins enrolments to courses and checks credits = 4. A student is returned only when no such enrolment exists, including students with no enrolments at all. It is safe with NULLs, unlike NOT IN.

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);
sql
SELECT s.name
FROM students s
WHERE NOT EXISTS (
  SELECT 1
  FROM enrolments e
  JOIN courses c ON c.id = e.course_id
  WHERE e.student_id = s.id AND c.credits = 4)
ORDER BY s.name;
name
Dara

The subquery asks, for each student, "is there an enrolment of this student in a 4-credit course?". NOT EXISTS keeps the students for whom the answer is no. Dara has no enrolments, so she qualifies. Ben took Databases, a 4-credit course, so he is excluded, and the same holds for everyone else: every student except Dara has at least one 4-credit course.

Changing the question slightly shows the pattern's flexibility. Students who took no course with fewer than 4 credits:

sql
SELECT s.name
FROM students s
WHERE NOT EXISTS (
  SELECT 1
  FROM enrolments e
  JOIN courses c ON c.id = e.course_id
  WHERE e.student_id = s.id AND c.credits < 4)
ORDER BY s.name;
name
Asha
Dara
Eli

The same anti-join written with LEFT JOIN works, but the join must include the credits filter in ON, not WHERE, or it turns into an inner join:

sql
SELECT s.name
FROM students s
LEFT JOIN (enrolments e JOIN courses c ON c.id = e.course_id AND c.credits = 4)
       ON e.student_id = s.id
WHERE e.student_id IS NULL
ORDER BY s.name;
name
Dara
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
FROM students s
WHERE NOT EXISTS (
  SELECT 1
  FROM enrolments e
  JOIN courses c ON c.id = e.course_id
  WHERE e.student_id = s.id AND c.credits = 4)
ORDER BY s.name;

SELECT s.name
FROM students s
WHERE NOT EXISTS (
  SELECT 1
  FROM enrolments e
  JOIN courses c ON c.id = e.course_id
  WHERE e.student_id = s.id AND c.credits < 4)
ORDER BY s.name;

SELECT s.name
FROM students s
LEFT JOIN (enrolments e JOIN courses c ON c.id = e.course_id AND c.credits = 4)
       ON e.student_id = s.id
WHERE e.student_id IS NULL
ORDER BY s.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • NOT EXISTS returns rows with no matching related row
  • The subquery can join and filter freely
  • Students with no enrolments at all are included
  • With a LEFT JOIN version, filters belong in ON

Common mistakes

  • Using NOT IN on a column that may contain NULL
  • Putting the credits filter in WHERE in the LEFT JOIN version

Follow-ups

  • How would you find students who took only 4-credit courses?
  • Which of these versions is fastest on your database?

Tips

  • Point out that students with no enrolments count; it is a frequent follow-up