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). Which ids does this query return? SELECT id FROM Bill b WHERE amt > (SELECT AVG(amt) FROM Bill x WHERE x.region = b.region);

  1. A. 3, 5 and 6
  2. B. 3, 6 and 7
  3. C. 3 and 6
  4. D. No rows

Correct answer: C. 3 and 6

E and W both average 375 over their non-NULL amounts, so only the 450 bills (3 and 6) pass. For Nair, x.region = NULL is never TRUE, the subquery's AVG is NULL and 200 > NULL is UNKNOWN; bill 7 has a NULL amt.

More DBMS and SQL questions

Exams that ask this topic