edited by
13,614 views
48 votes
48 votes

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:

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

edited by

2 Answers

Best answer
54 votes
54 votes
The inner query will return $$\begin{array} {c c} \hline \textbf{DeptName} & \textbf{Num} \\\hline  \text{AA} & 4 \\\hline \text{AB} & 3 \\\hline \text{AC} & 3 \\\hline  \text{AD} & 2 \\\hline\text{AE} & 1  \end{array}$$ Now $\text{AVG(EC.Num)}$ will find the average of Num values in the above-returned query, which is $(4+3+3+2+1)\div 5=2.6$

So according to me, the answer should be $2.6.$
edited by
18 votes
18 votes

solution.............

Answer:

Related questions

52 votes
52 votes
5 answers
1
Arjun asked Feb 14, 2017
12,918 views
Consider a database that has the relation schema CR(StudentName, CourseName). An instance of the schema CR is as given below.$$\begin{array}{|c|c|} \hline \textbf{Student...