UGC-NET Jan 2017 · SQL Queries

MCQ · 2 marks · Problem DBMS-SQL-0014

Consider the following relation:Works(emp_name, company_name,salary) Here,emp name is primary key. Consider the following SQL querySelect emp name From works T where salary > (select avg(salary)                from works S                where  T. company name =                       S. company name) The above query is for following:
  1. A
    Find the highest paid employee who earns more  than the average salary of all employees of his company.
  2. B
    Find the highest paid employee who earns more  than the average salary of all  the employees of all the companies.
  3. C
    Find all employees who earn more than the average salary of all employees all the companies.
  4. D
    Find all employees who earn more than the average salary of all employees of their company.

Solve it on GATE DA practice →More Relational Model & SQL problems

Show the worked solutionThe subquery is correlated: for each employee T it computes the average salary of the employees S in the same company as T. The outer query keeps T if T's salary exceeds that average. So it lists all employees who earn more than the average salary of their own company.(A) and (B) Nothing picks a single highest-paid employee — incorrect.(C) The average is per company, not over all companies — incorrect.Answer: D

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com