GATE / CS ARCHIVE

GATE CSE 2021 SET 2 / QUESTION 31

GATE CSE 2021 Set 2 Q31

Database Management Systems · SQL & Queries · Full GATE CSE 2021 Set 2 paper

GATE 2021 Set 2, Q31 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