← Back to SQL, page 7

sql-0129 · Easy fresher

Write a query that finds students enrolled in both Databases (10) and Algorithms (20) using a set operation.

INTERSECT returns rows present in both results: SELECT student_id FROM enrolments WHERE course_id = 10 INTERSECT SELECT student_id ... WHERE course_id = 20. It removes duplicates, like UNION. A self join or GROUP BY with HAVING COUNT(DISTINCT course_id) = 2 gives the same answer.

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 student_id FROM enrolments WHERE course_id = 10
INTERSECT
SELECT student_id FROM enrolments WHERE course_id = 20
ORDER BY student_id;
student_id
1
3

Asha (1) and Chen (3) took both courses. Ben took only Databases and Eli only Algorithms.

The GROUP BY version scales better to "all of these N courses", because only the list and the count change:

sql
SELECT student_id
FROM enrolments
WHERE course_id IN (10, 20)
GROUP BY student_id
HAVING COUNT(DISTINCT course_id) = 2
ORDER BY student_id;
student_id
1
3

INTERSECT is concise for two or three sets. For "students who took every course in a list", the HAVING form is easier to extend. Both forms compare whole rows, so select only the key columns you want to match on.

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
INTERSECT
SELECT student_id FROM enrolments WHERE course_id = 20
ORDER BY student_id;

SELECT student_id
FROM enrolments
WHERE course_id IN (10, 20)
GROUP BY student_id
HAVING COUNT(DISTINCT course_id) = 2
ORDER BY student_id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • INTERSECT keeps rows found in both results
  • It removes duplicates by default
  • GROUP BY with HAVING COUNT(DISTINCT ...) generalizes to N values
  • Select only the columns that define a match

Common mistakes

  • Writing WHERE course_id = 10 AND course_id = 20, which matches nothing
  • Intersecting full rows that include columns that differ, such as marks

Follow-ups

  • How would you find students in either course but not both?
  • How would you return student names instead of ids?

Tips

  • Show both forms and say when each fits