GATE / CS ARCHIVE

GATE CSE 2017 SET 1 / QUESTION 23

GATE CSE 2017 Set 1 Q23

Database Management Systems · SQL & Queries · Full GATE CSE 2017 Set 1 paper

GATE 2017 Set 1, Q23 1 mark · NAT
Report issue

SQL & Queries

Question 23, GATE 2017: EMP(EmpId,EmpName,DeptName) has rows with IDs 1 through 13, names XYA through XYM, and departments AA,AA,AA,AA,AB,AB,AB,AC,AC,AC,AD,AD,AE respectively. The source SQL is:
SELECT AVG(EC.Num) FROM EC WHERE (DeptName, Num) IN
(SELECT DeptName, COUNT(EmpId) AS EC(DeptName, Num) FROM EMP GROUP BY DeptName)
Find its output. This unusual source syntax is retained; the solution distinguishes the intended aggregate from the literal SQL.
Tap to enlarge
Answer & solution

Official answer: 2.6

Our answer: 2.6 under intended grouped-table query

1. For the intended group operation, collect all rows with equal DeptName. The counts are AA:4, AB:3, AC:3, AD:2, AE:1.

2. AVG must average these five group counts, not average EmpId values or take an ungrouped COUNT. Their sum is 13, so the average is 13/5=2.6.

3. That derives the official intended numeric answer, but not an execution of the literal SQL. EC is not introduced as a proper relation/CTE, and AS EC(DeptName,Num) is not a valid ordinary COUNT alias. A corrected query must define EC before SELECT AVG(Num) FROM EC.

Original paper, page 12 · Answer key