CS FundamentalsVery hardGATE CS · SQL

Table Score(name, pts) rows: ('a', 90), ('b', 85), ('c', 90), ('d', 70), ('e', 85), ('f', 60). What does this query return?

Table Score(name, pts) rows: ('a', 90), ('b', 85), ('c', 90), ('d', 70), ('e', 85), ('f', 60). What does this query return?
SELECT name FROM Score s
WHERE 2 = (SELECT COUNT(DISTINCT pts) FROM Score t
           WHERE t.pts > s.pts)
ORDER BY name;
  1. A. a, c
  2. B. f
  3. C. b, e
  4. D. d

Correct answer: D. d

A row qualifies when exactly two DISTINCT scores are higher than it. Above 70 are only 90 and 85, so d (the third-highest distinct score) is returned. With COUNT(*) instead, b and e (two 90s above 85) would be returned.

More DBMS and SQL questions

Exams that ask this topic