Question 1
Department average salaries are: dept 10 = 7850, dept 20 = 6100, dept 30 ≈ 5733; the overall average across all 8 employees is 6400. How many rows does the following query return?
SELECT dept_id
FROM employees
GROUP BY dept_id
HAVING AVG(salary) > (SELECT AVG(salary) FROM employees)A. 3
3 is the number of departments (groups) before the HAVING filter. HAVING removes the two departments whose average is below the overall average, leaving 1.
B. 2
Only department 10 (average 7850) exceeds the overall average of 6400. Departments 20 (6100) and 30 (≈5733) are both below it, so 2 is an overcount — only 1 group qualifies.
C. The statement raises an error because a subquery cannot appear in a HAVING clause.
A subquery is permitted in the HAVING clause; comparing a group's aggregate against a scalar subquery is valid. No error occurs, and exactly one department's average exceeds the overall average.
D. 1Correct answer
The scalar subquery returns the overall average, 6400. HAVING then keeps only groups whose department average exceeds 6400: dept 10 (7850) qualifies, while dept 20 (6100) and dept 30 (≈5733) do not. Exactly 1 group row is returned.
Explanation
The HAVING clause filters groups after aggregation, and it may compare a group's aggregate against a scalar subquery. Here each department's AVG(salary) is compared to the overall average (the subquery result, 6400). Only department 10 (7850) exceeds it; departments 20 (6100) and 30 (≈5733) do not, so a single group row is returned. Using a subquery in HAVING is fully supported — it is the idiomatic way to compare group aggregates against an overall or reference value.