CS FundamentalsHardGATE CS (PYQ pattern)

Table W(id, dept, sal): (1,A,50), (2,A,70), (3,A,90), (4,B,40), (5,B,40), (6,C,100), (7,C,20), (8,C,NULL), (9,D,NULL). SELECT COUNT(*) FROM W e WHERE sal > (SELECT AVG(sal) FROM W x WHERE x.dept = e.dept); What does it return?

  1. A. 3
  2. B. 5
  3. C. 2
  4. D. 4

Correct answer: C. 2

Department averages (NULLs skipped): A = 70, B = 40, C = 60, D = NULL. Rows above their own average: id 3 (90) and id 6 (100) = 2. Id 2 (70) and the B rows equal their average (not >), ids 8 and 9 compare NULL. A global average would give 3 rows and >= would give 5.

More DBMS and SQL questions

Exams that ask this topic