CS FundamentalsHardGATE CS (PYQ pattern)

SL(r, q, amt): (N,Q1,10), (N,Q2,20), (S,Q1,5), (S,Q1,15), (S,Q2,NULL), (E,Q2,7). The query SELECT r, SUM(CASE WHEN q = 'Q1' THEN amt END) AS a, SUM(CASE WHEN q = 'Q2' THEN amt ELSE 0 END) AS b FROM SL GROUP BY r is run. How many result rows have a IS NULL, and what is the sum of the b column?

  1. A. 1 row; sum of b = 28
  2. B. 2 rows; sum of b = 27
  3. C. 1 row; sum of b = 27
  4. D. 0 rows; sum of b = 27

Correct answer: C. 1 row; sum of b = 27

Group E has no Q1 row, so CASE gives NULL for every row and SUM is NULL (1 row with a NULL). For b the ELSE 0 makes NULL amounts irrelevant: 20 + 0 + 7 = 27.

More DBMS and SQL questions

Exams that ask this topic