Skip to main content

GATE CSE 2021Databases › SQLVerified

GATE CSE 2021 PYQ: The relation scheme given below is used to store information about the employees of a company, where empId…

The relation scheme given below is used to store information about the employees of a company, where empId\textsf{empId} is the key and deptId\textsf{deptId} indicates the department to which the employee is assigned. Each employee is assigned to exactly one department.

emp(empId‾, name, gender, salary, deptId)\textsf{emp(}\underline{\textsf{empId}}\textsf{, name, gender, salary, deptId)}

Consider the following SQL\text{SQL} query:

  1. select deptId, count(*)
  2. from emp
  3. where gender = “female” and salary > (select avg(salary)from emp)
  4. 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

  1. A
    employees in the department
  2. B
    employees in the company
    Correct answer
  3. C
    female employees in the department
  4. D
    female employees in the company

Answer

(B) employees in the company

Approach

Identify whether the inner subquery is correlated or independent. Since it has no reference to the outer emp row and no deptId condition, it computes one global average salary over all employees in emp. The outer query then counts female employees, grouped by department, whose salary exceeds that global average.

The subquery is independent and evaluates avg(salary) over all rows of emp without any gender or department restriction. Therefore the outer query counts female employees per department whose salary exceeds the company-wide average salary.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.