Skip to main content

GATE CSE 2017Databases › SQLVerified

GATE CSE 2017 PYQ: Consider a database that has the relation schema EMP (EmpId, EmpName, and DeptName). An instance of the…

Consider a database that has the relation schema EMP (EmpId, EmpName, and DeptName). An instance of the schema EMP and a SQL query on it are given below:

EmpIdEmpNameDeptName1XYAAA2XYBAA3XYCAA4XYDAA5XYEAB6XYFAB7XYGAB8XYHAC9XYIAC10XYJAC11XYKAD12XYLAD13XYMAEEMP\small \overset{\text{EMP}}{\begin{array}{|c|c|c|}\hline \textbf{EmpId}& \textbf{EmpName}& \textbf{DeptName}\\\hline 1& \text{XYA}& \text{AA} \\ \hline 2& \text{XYB}& \text{AA} \\ \hline 3& \text{XYC}& \text{AA} \\\hline 4& \text{XYD}& \text{AA} \\\hline 5& \text{XYE}& \text{AB} \\\hline 6& \text{XYF}& \text{AB} \\\hline 7& \text{XYG}& \text{AB} \\\hline 8& \text{XYH}& \text{AC} \\\hline 9& \text{XYI}& \text{AC} \\\hline 10& \text{XYJ}& \text{AC} \\\hline 11&\text{XYK} & \text{AD} \\\hline 12 & \text{XYL}& \text{AD} \\\hline 13 & \text{XYM} & \text{AE} \\\hline\end{array}}

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 _____________ .

    Answer

    2.6

    Approach

    Evaluate the inner grouped query first to get one row per department with the department's employee count, then take the average of those count values.

    The only values averaged are the per-department employee counts: 4, 3, 3, 2, and 1. Their sum is 13 and there are 5 departments, so the required average is 13/5 = 2.6.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.