The employee information in a company is stored in the relation
- Employee (name, sex, salary, deptName)
Consider the following SQL query
Select deptName
From Employee
Where sex = ‘M’
Group by deptName
Having avg(salary) >
(select avg (salary) from Employee)It returns the names of the department in which
View worked solution
Answer: D
To understand what the SQL query returns, let us break it down step-by-step:
-
The
WHEREclause:
This filters theWhere sex = 'M'Employeerelation to retain only male employees before any grouping takes place. -
The
GROUP BYclause:
This groups the filtered male employees by their department (Group by deptNamedeptName). -
The
HAVINGclause:
Since the grouping is done on the filtered set (only male employees),Having avg(salary) > ...avg(salary)here computes the average salary of male employees in each department. -
The Subquery:
This subquery has no(select avg (salary) from Employee)WHEREclause, so it computes the average salary of all employees in the entire company.
Combining these parts, the query selects the deptName of departments where the average salary of male employees is greater than the average salary of all employees in the company.
Therefore, the correct option is D.
Free practice · Your answers on this page are not saved.
