← Back to SQL, page 4

sql-0064 · Medium mid

Write a query to find students enrolled in both Databases and Algorithms.

Filter enrolments to the two courses, group by student, and keep groups where COUNT(DISTINCT course_id) = 2. INTERSECT of the two student lists also works. A WHERE course_id = 10 AND course_id = 20 is always false, because one row cannot hold two values at once.

Full answer

The tempting first attempt does not work:

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 student_id FROM enrolments WHERE course_id = 10 AND course_id = 20;
student_id

(0 rows)

WHERE looks at one row at a time, and no single enrolment row is in both courses. The condition is about a student across several rows, so it needs grouping:

sql
SELECT s.name
FROM enrolments e
JOIN students s ON s.id = e.student_id
WHERE e.course_id IN (10, 20)
GROUP BY s.id, s.name
HAVING COUNT(DISTINCT e.course_id) = 2
ORDER BY s.name;
name
Asha
Chen

COUNT(DISTINCT e.course_id) = 2 means "this student has rows for both courses". DISTINCT protects against duplicate enrolment rows if the table allowed them.

INTERSECT gives the same answer by taking the students common to both lists:

sql
SELECT s.name FROM students s WHERE s.id IN (
  SELECT student_id FROM enrolments WHERE course_id = 10
  INTERSECT
  SELECT student_id FROM enrolments WHERE course_id = 20
)
ORDER BY s.name;
name
Asha
Chen

The grouping version generalises better. "At least two of these three courses" becomes HAVING COUNT(DISTINCT course_id) >= 2, which INTERSECT cannot express. Changing it to "all of the listed courses" is a form of relational division, which comes up as its own interview question.

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 student_id FROM enrolments WHERE course_id = 10 AND course_id = 20;

SELECT s.name
FROM enrolments e
JOIN students s ON s.id = e.student_id
WHERE e.course_id IN (10, 20)
GROUP BY s.id, s.name
HAVING COUNT(DISTINCT e.course_id) = 2
ORDER BY s.name;

SELECT s.name FROM students s WHERE s.id IN (
  SELECT student_id FROM enrolments WHERE course_id = 10
  INTERSECT
  SELECT student_id FROM enrolments WHERE course_id = 20
)
ORDER BY s.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • A WHERE condition sees one row at a time
  • Conditions across several rows need GROUP BY and HAVING
  • COUNT(DISTINCT course) = number of required courses
  • INTERSECT is an alternative for exactly two lists

Common mistakes

  • Writing course_id = 10 AND course_id = 20
  • Using OR and returning students in either course

Follow-ups

  • How would you find students in Databases but not Algorithms?
  • How would you find students enrolled in every course?

Tips

  • Explain why the AND version is always empty; it shows you understand row-wise evaluation