GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATABASE MANAGEMENT SYSTEMS

Distinguish company-average SQL subqueries from grouped averages

2 questions solved the same way, newest first. Back to Database Management Systems

How these are solved

Evaluate the uncorrelated AVG subquery over the whole company separately from the outer gender filter and department grouping. In 2004 HAVING compares a male-group average; in 2021 WHERE filters individual female salaries and COUNT aggregates survivors.

2 marks · MCQ
Report issue

SQL & Queries

Question 31, GATE 2021: Schema emp(empId,name,gender,salary,deptId), key empId, each employee in one department. Query: select deptId,count(*) from emp where gender="female" and salary>(select avg(salary) from emp) group by deptId. Counts female employees above which mean? A employees in department; B employees in company; C women in department; D women in company.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

The uncorrelated subquery averages salary across the entire emp relation, with no gender or department filter. The outer query counts female employees above that company-wide average, grouped by department. Thus B. Departments with no qualifying rows are omitted rather than returned with count 0. The printed double-quoted female is understood as an intended string literal; standard SQL uses single quotes, while double quotes ordinarily delimit identifiers.

The inner SELECT has no reference to the outer row, so its scalar mean is computed over all emp rows. The gender test is outside it and does not restrict that mean. GROUP BY affects the final count, not the subquery's population. This distinction separates all four options.

Original paper, page 72 · Answer key

2 marks · MCQ
Report issue

Grouped salary comparison with global average

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