CS FundamentalsHardGATE CS

Tk(s, c) = (s1, c1), (s1, c2), (s1, c3), (s2, c1), (s2, c2), (s3, c1), (s3, c2), (s3, c3), (s3, c4), (s4, c3), (s5, c2), (s5, c3), (s5, c1), (s5, c3) (note the repeated row for s5) and Cr(c) = (c1), (c2), (c3). How many students does SELECT DISTINCT s FROM Tk t WHERE NOT EXISTS (SELECT * FROM Cr WHERE NOT EXISTS (SELECT * FROM Tk t2 WHERE t2.s = t.s AND t2.c = Cr.c)) return?

  1. A. 1
  2. B. 2
  3. C. 3
  4. D. 4

Correct answer: C. 3

The double NOT EXISTS is relational division: it returns students taking every course in Cr, here s1, s3 and s5 (3). The shortcut HAVING COUNT(*) = 3 would return only 1 student, because s3 has four rows and s5 has a duplicate row.

More DBMS and SQL questions

Exams that ask this topic