CS FundamentalsModerateGATE 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 AVG(amt), AVG(COALESCE(amt, 0)) FROM Bill;

  1. A. 340 and about 242.86
  2. B. about 242.86 and 340
  3. C. about 283.33 and about 242.86
  4. D. 340 and 340

Correct answer: A. 340 and about 242.86

AVG(amt) ignores the 2 NULLs: 1700 / 5 = 340. COALESCE turns them into 0, so the second average divides 1700 by all 7 rows (about 242.86).

More DBMS and SQL questions

Exams that ask this topic