Question
Consider a database that has the relation schema . An instance of the schema EMP and a SQL query on it are given below.
| EmpId | EmpName | DeptName |
|---|---|---|
| 1 | XYA | AA |
| 2 | XYB | AA |
| 3 | XYC | AA |
| 4 | XYD | AA |
| 5 | XYE | AB |
| 6 | XYF | AB |
| 7 | XYG | AB |
| 8 | XYH | AC |
| 9 | XYI | AC |
| 10 | XYJ | AC |
| 11 | XYK | AD |
| 12 | XYL | AD |
| 13 | XYM | AE |
SELECT AVG(EC.Num)
FROM EC
WHERE (DeptName, Num) IN
(SELECT DeptName, COUNT(EmpId) AS EC(DeptName, Num)
FROM EMP
GROUP BY DeptName)
The output of executing the SQL query is ______.
Official answer and solution
- Official answer
- 2.6
- From
- Official GATE 2017 answer key
Solution
Group the EMP table by DeptName, count the number of employees per department, and compute their arithmetic mean.
The inner query groups the EMP table by DeptName and counts the number of employees in each department:
SELECT DeptName, COUNT(EmpId) AS Num
FROM EMP
GROUP BY DeptName
Counting the rows for each distinct DeptName from the EMP relation:
AA: EmpId 1, 2, 3, 4 employeesAB: EmpId 5, 6, 7 employeesAC: EmpId 8, 9, 10 employeesAD: EmpId 11, 12 employeesAE: EmpId 13 employee
This yields 5 department counts: 4, 3, 3, 2, 1.
The outer query computes the average of these department counts (AVG(EC.Num)):
2.6
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2017 Computer Science and Information Technology (CS) Session 1
- Question
- 23 of 65, printed as CS Q.23
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.