CS FundamentalsHardGATE CS · Amazon/Microsoft SDE MCQ

Table Bill(id, cust, region, amt) rows: (1, 'Kavi', 'E', 300), (2, 'Lata', 'W', NULL), (3, 'Kavi', 'E', 450), (4, 'Mona', 'W', 300), (5, 'Nair', NULL, 200), (6, 'Lata', 'W', 450), (7, 'Mona', 'E', NULL). What does this query return? SELECT SUM(CASE WHEN amt > 300 THEN 1 WHEN amt <= 300 THEN 0 ELSE -1 END) FROM Bill;

  1. A. 5
  2. B. -2
  3. C. 0
  4. D. NULL

Correct answer: C. 0

Bills 3 and 6 give 1 each, bills 1, 4 and 5 give 0, and for the two NULL amounts both comparisons are UNKNOWN, so CASE falls to ELSE -1. Total 2 + 0 - 2 = 0; CASE never returns NULL here.

More DBMS and SQL questions

Exams that ask this topic