464 views
4 votes
4 votes

Consider the following relations.$$\textbf{Emp}$$$$\begin{array}{|c|c|c|c|c|c|c|} 
\hline
\underline{\textbf{EmpId}} & \textbf{DeptNo} & \textbf{EmpName} & \textbf{Salary} \\
\hline
1 & 5 & \text{Alex}&10,000 \\
\hline
2 & 2 & \text{Steve}&20,000 \\
\hline
3 & 1 & \text{Bob} &15,000 \\
\hline
4 & 2 & \text{Smith}&18,000 \\
\hline
5 & 4 & \text{Rakul}&24,000 \\
\hline
6 & 3 & \text{Rhea}&17,000 \\
\hline
\end{array}$$
$$\textbf{Dept}$$$$\begin{array}{|c|c|c|c|c|c|c|}
\hline
\underline{\textbf{DeptId}} & \textbf{DeptName} \\
\hline
1 & \text{CSE} \\
\hline
2 & \text{ECE} \\
\hline
3 & \text{ECE} \\
\hline
4 & \text{IT} \\
\hline
5 & \text{ME} \\
\hline
\end{array}$$

Here, Emp(DeptNo) is the foreign key referring to Dept(DeptId).

What does the following SQL query give? (Mark all the appropriate options)

SELECT Emp.EmpId, Emp.EmpName FROM Emp,Dept 
    WHERE Emp.DeptNo = Dept.DeptId 
    group by Dept.DeptId having Dept.DeptName = 'ECE'
  1. It gives the id, name of the employees who works for 'ECE' department
  2. It gives the name of the employees whose DeptNo is 2
  3. It gives the id,name of the employees whose DeptNo is either 2 or 3
  4. The query has error

2 Answers

Best answer
13 votes
13 votes
During a group by, we cannot include any attribute in the select which is not part of the group by. So, select EmpId is wrong here so is select EmpName. But we can do select max(EmpId), DeptId.
selected by
1 votes
1 votes
All non-aggregate functions that appear in SELECT clause must be mentioned in GROUPBY clause,otherwise error,

where as aggregate functions can be applied to any attributes.
Answer:

Related questions

2 votes
2 votes
1 answer
1
gatecse asked Oct 8, 2020
274 views
Consider the following relation instance.$$\textbf{Student}$$$$\begin{array}{|c|c|c|c|c|}\hline \textbf{Roll}& \textbf{FirstName} & \textbf{LastName} & \textbf{Weight}\\\...
1 votes
1 votes
1 answer
2
gatecse asked Oct 8, 2020
289 views
Given the following relation $R$$$\textbf{R}$$$$\begin{array}{|c|c|c|c|c|c|c|}\hline \textbf{A} & \textbf{B} \\\hline1 & \text{NULL}\\ \hline1 & \text{NULL}\\ \hline\text...
4 votes
4 votes
1 answer
3
gatecse asked Oct 8, 2020
368 views
Consider the following relation $R$$$\textbf{R}$$$$\begin{array}{|c|c|c|c|c|c|c|}\hline\textbf{A} & \textbf{B} \\\hline1 & \text{NULL} \\\hline1 & \text{NULL} \\\hline\te...
3 votes
3 votes
1 answer
4
gatecse asked Oct 8, 2020
349 views
Consider the following relation.$$\textbf{Emp}$$$$\begin{array}{|c|c|c|c|c|c|c|}\hline\underline{\textbf{EmpId}} & \textbf{DeptNo} & \textbf{EmpName} & \textbf{Salary} \\...