GATE / CS ARCHIVE

GATE CSE 2004 / QUESTION 53

Grouped salary comparison with global average

Database Management Systems · SQL & Queries · Full GATE CSE 2004 paper

GATE 2004, Q53 2 marks · MCQ
Report issue

SQL & Queries

Question 53, GATE 2004: Employee(name,sex,salary,deptName) has key name. What department names does this SQL query return?
SELECT deptName FROM Employee
WHERE sex='M'
GROUP BY deptName
HAVING AVG(salary) > (SELECT AVG(salary) FROM Employee);
Options:
(A) Departments whose average salary exceeds the company average
(B) Departments whose male average salary exceeds the company-wide male average
(C) Departments whose male average salary exceeds the average of all employees in that department
(D) Departments whose male average salary exceeds the company average
Question 53, GATE 2004: Employee(name,sex,salary,deptName) has key name. What department names does this SQL query return?
SELECT deptName FROM Employee
WHERE sex='M'
GROUP BY deptName
HAVING AVG(salary) > (SELECT AVG(salary) FROM Employee);
Options:
(A) Departments whose average salary exceeds the company average
(B) Departments whose male average salary exceeds the company-wide male average
(C) Departments whose male average salary exceeds the average of all employees in that department
(D) Departments whose male average salary exceeds the company average
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. WHERE sex=M filters the outer rows to male employees before grouping by department. HAVING compares each such group’s average salary with the uncorrelated subquery SELECT AVG(salary) FROM Employee, which averages all employees company-wide.

2. Therefore it returns departments whose male employees’ average exceeds the overall company average. The inner query has no male filter and is not restricted to the same department.

3. Teaching check: Read SQL in stages: WHERE keeps only males; GROUP BY makes one male group per department; HAVING compares its average against a separately computed company-wide average. There is no correlation predicate linking the subquery to the department, and no sex filter inside it.

Original paper, page 7