CS FundamentalsHardGATE CS

Emp(id, dept, sal) = (1, A, 50), (2, A, 70), (3, A, NULL), (4, B, 40), (5, B, 60), (6, B, 60), (7, NULL, 90), (8, C, 30). How many rows does SELECT id FROM Emp e1 WHERE sal > (SELECT AVG(sal) FROM Emp e2 WHERE e2.dept = e1.dept AND e2.id <> e1.id) return?

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

Correct answer: D. 3

For each row the subquery averages the other employees of the same department. NULL departments match nothing (the subquery is empty, so AVG is NULL), a NULL salary never satisfies the comparison, and a lone employee in a department also fails. Only the rows with a salary strictly above the average of their colleagues qualify: 3 rows.

More DBMS and SQL questions

Exams that ask this topic