CS FundamentalsVery hardGATE CS · SQL

Table Stu(sid, sname) rows: (1, 'Om'), (2, 'Pia'), (3, 'Raj'), (4, 'Sia'). Table Takes(sid, cid, marks) rows: (1, 'C1', 80), (1, 'C2', NULL), (2, 'C1', 65), (3, 'C2', 90), (3, 'C3', 75), (5, 'C1', 50). What does this query return?

Table Stu(sid, sname) rows: (1, 'Om'), (2, 'Pia'), (3, 'Raj'), (4, 'Sia'). Table Takes(sid, cid, marks) rows: (1, 'C1', 80), (1, 'C2', NULL), (2, 'C1', 65), (3, 'C2', 90), (3, 'C3', 75), (5, 'C1', 50). What does this query return?
SELECT sname FROM Stu s
WHERE NOT EXISTS (
  SELECT cid FROM Takes p
  WHERE p.sid = 1
    AND NOT EXISTS (SELECT * FROM Takes t
                    WHERE t.sid = s.sid AND t.cid = p.cid))
ORDER BY sname;
  1. A. Om
  2. B. Om, Pia, Raj
  3. C. Om, Raj
  4. D. Om, Pia

Correct answer: A. Om

This is relational division: students who took every course that student 1 (Om) took, i.e. both C1 and C2. Pia lacks C2 and Raj lacks C1, so only Om remains.

More DBMS and SQL questions

Exams that ask this topic