CS FundamentalsVery hardGATE CS

T(g, v) = (g1, 5), (g1, 15), (g2, 8), (g2, 9), (g2, NULL), (g3, 50), (g4, NULL), (g4, NULL), (g5, 12), (g5, 3). How many groups does SELECT g FROM T GROUP BY g HAVING SUM(CASE WHEN v > 10 THEN 1 END) IS NULL OR MAX(v) > 40 return?

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

Correct answer: B. 3

CASE without ELSE yields NULL when the condition is false or UNKNOWN, so SUM over a group where no row exceeds 10 (or all v are NULL) is NULL. That holds for g2 and g4, and g3 passes by MAX(v) > 40: 3 groups. Adding ELSE 0 would make the sums 0 instead of NULL and give 1.

More DBMS and SQL questions

Exams that ask this topic