in Databases edited by
8,256 views
35 votes
35 votes

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

  1. the average salary is more than the average salary in the company

  2. the average salary of male employees is more than the average salary of all male employees in the company

  3. the average salary of male employees is more than the average salary of employees in same the department

  4. the average salary of male employees is more than the average salary in the company

in Databases edited by
8.3k views

4 Comments

$(A)$the average salary is more than the average salary in the company.

What is the meaning of this option?
0
0

@Lakshman Patel RJIT a company can have both male and female employ so this option means the average salary of departmeant (uncluding both female and male )    rest are same as d option 

1
1

Can we have HAVING salary instead of HAVING avg(salary) here???

0
0

"Any attribute that is present in the having
clause without being aggregated must appear in the group by clause, otherwise
the query is treated as erroneous." Korth 6th edition chapter 3 , page 88.

2
2

3 Answers

29 votes
29 votes
Best answer
D is the answer.

The inner query is over all department and over both male and female employees while the outer query is only for male employees.
selected by
by

4 Comments

I am not getting how group by clause has no significance here?
1
1

@Cristina6884 here group by 

grouping all male according to department so the avg we calculated is the avg of male salary in that department

0
0
Here group by clause is used to support having clause...
0
0
4 votes
4 votes
In outer query :the average salary of male employees in each department

inner query:  the average salary in the company

ans d
by

3 Comments

Answer should be Department names in which Avg Salary of male employees in that department is greater than Avg salary of all employees in the table
0
0

In outer query :the average salary of male employees in each department 

inner query:  the average salary in the company

 

options d does not specify in each department so i think answer d also wrong

1
1
"The names of the department in which" is already mentioned in the question..
0
0
0 votes
0 votes
A) the names of the department in which the average salary is more than the average salary in the company

select deptName

          from Employee

          Group by deptName

          Having avg(salary) > ( select avg(salary) from Employee )

B)the names of the department in which the average salary of male employees is more than the average salary of all male employees in the company

select deptName

      from Employee

      where sex=’M’

      Group by deptName

      Having avg (salary) > ( select avg(salary) from Employee where SEX=”M” )

C)the names of the department in which  the average salary of male employees is more than the average salary of employees in same the department

select deptName

         from Employee  e

         where sex=”M”

        Group by deptName

         Having avg(salary) > ( select avg(salary) from Employee ee where ee.deptName = e.deptName )

 

  D)the names of the department in which the average salary of male employees is more than the average salary in the company

same as querry provided in question.
Answer:

Related questions