The relation scheme given below is used to store information about the employees of a company, where is the key and indicates the department to which the employee is assigned. Each employee is assigned to exactly one department.
Consider the following query:
select deptId, count(*) from emp where gender = “female” and salary > (select avg(salary)from emp) group by deptId;
The above query gives, for each department in the company, the number of female employees whose salary is greater than the average salary of
View worked solution
Answer: B
To understand what the SQL query computes, let us analyze the subquery and the outer query step-by-step:
-
The Subquery:
This subquery calculates the average salary of all tuples in the(select avg(salary) from emp)emprelation. Since there is noWHEREclause or correlation with the outer query, it computes the average salary of all employees in the company. -
The Outer Query's
WHEREClause:
This filters thewhere gender = "female" and salary > (subquery)emprelation to keep only those employees who are:- Female (
gender = "female"), AND - Have a salary strictly greater than the average salary of all employees in the company.
- Female (
-
The
GROUP BYandSELECTClause:
This groups the filtered female employees by their department (select deptId, count(*) from ... group by deptId;deptId) and counts how many such employees exist in each department.
Thus, the query returns the number of female employees in each department whose salary is greater than the average salary of employees in the company.
Therefore, the correct option is B.
Free practice · Your answers on this page are not saved.
