UGC-NET Jun 2013 · SQL Queries

MCQ · 2 marks · Problem DBMS-SQL-0016

The employee information of an organization is stored in a relation: Employee (name, sex, salary, deptname). Consider the following SQL query:Select deptname from Employee where sex=’M’ group by dept name having avg (salary) > {select avg (salary) from Employee} Output of the given query corresponds to
  1. A
    Average salary of employee more than average salary of the organization
  2. B
    Average salary of employee less than average salary of the organization
  3. C
    Average salary of employee equal to average salary of the organization
  4. D
    Average salary of male employees in a department is more than average salary of the organization

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

Show the worked solutionWHERE sex = 'M' keeps only male employees before grouping. GROUP BY deptname then forms one group per department, and HAVING compares each group's average (the male average in that department) with the subquery, which is the average salary over the whole organisation. So the query lists the departments where male employees' average salary exceeds the organisation's average salary.(A), (B), (C) ignore the male filter or reverse the comparison — incorrect.Answer: D

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