CS FundamentalsVery hardGATE CS (PYQ pattern)

E1(id, d, s): (1,x,50), (2,x,70), (3,x,70), (4,y,40), (5,y,40), (6,z,90), (7,z,NULL), (8,w,NULL). How many rows does SELECT * FROM E1 a WHERE NOT EXISTS (SELECT 1 FROM E1 b WHERE b.d = a.d AND b.s > a.s) return?

  1. A. 5
  2. B. 7
  3. C. 8
  4. D. 6

Correct answer: B. 7

A row survives if no row of its own department has a strictly larger s (comparisons with NULL are not true). Ties survive (ids 2, 3 and 4, 5), 6 survives, and rows with s NULL survive too because b.s > NULL is unknown: ids 1 loses to 70, giving 7 rows. The MAX-based query would return 5 because NULL = NULL is unknown.

More DBMS and SQL questions

Exams that ask this topic