CS FundamentalsModerateAmazon/Flipkart SQL round · GATE CS

Table Staff(id, name, dept, pay, boss) rows: (1, 'Anu', 'Ops', 40, NULL), (2, 'Bala', 'Ops', 55, 1), (3, 'Chitra', 'Tech', 70, 1), (4, 'Dev', 'Tech', NULL, 3), (5, 'Esha', NULL, 35, 3), (6, 'Farid', 'Tech', 70, 2). What does this query return?

Table Staff(id, name, dept, pay, boss) rows: (1, 'Anu', 'Ops', 40, NULL), (2, 'Bala', 'Ops', 55, 1), (3, 'Chitra', 'Tech', 70, 1), (4, 'Dev', 'Tech', NULL, 3), (5, 'Esha', NULL, 35, 3), (6, 'Farid', 'Tech', 70, 2). What does this query return?
SELECT AVG(pay), AVG(COALESCE(pay, 0))
FROM Staff;
  1. A. 45, 45
  2. B. 54, 54
  3. C. 45, 54
  4. D. 54, 45

Correct answer: D. 54, 45

AVG ignores the NULL pay: 270/5 = 54. COALESCE turns it into 0, so the second average is 270/6 = 45.

More DBMS and SQL questions

Exams that ask this topic