CS FundamentalsVery hardProduct-company MCQ pattern

Table Enroll(sid, cid) rows: (1, 'C1'), (1, 'C2'), (2, 'C1'), (3, 'C1'), (3, 'C2'), (3, 'C3'), (4, 'C3'). Table Course(cid) rows: ('C1'), ('C2'), ('C3'). What does this query return?

Table Enroll(sid, cid) rows: (1, 'C1'), (1, 'C2'), (2, 'C1'), (3, 'C1'), (3, 'C2'), (3, 'C3'), (4, 'C3'). Table Course(cid) rows: ('C1'), ('C2'), ('C3'). What does this query return?
SELECT DISTINCT sid FROM Enroll e
WHERE NOT EXISTS (
  SELECT * FROM Course c
  WHERE NOT EXISTS (
    SELECT * FROM Enroll x
    WHERE x.sid = e.sid AND x.cid = c.cid));
  1. A. 1, 2, 3 and 4
  2. B. 1 and 3
  3. C. No rows
  4. D. 3

Correct answer: D. 3

This double NOT EXISTS is relational division: students for whom there is no course they have not taken, i.e. students enrolled in ALL courses. Only student 3 has C1, C2 and C3; student 1 lacks C3.

More DBMS and SQL questions

Exams that ask this topic