简体   繁体   中英

How do I find departments with higher average salary than the average salary of the company?

I am trying to output only departments with a higher average salary than the average salary of the company.

SELECT Departments.DEPARTMENT_NAME
FROM Employees inner join departments
ON Employees.DEPARTMENT_ID = Departments.DEPARTMENT_ID
WHERE (SELECT AVG(CAST(Employees.salary AS decimal)) 
      GROUP BY Employees.DEPARTMENT_ID) > (SELECT AVG(CAST(Employees.salary AS decimal)))
GROUP BY Departments.DEPARTMENT_NAME; 

I think this might be what you want:

SELECT D.DEPARTMENT_NAME
FROM Employees E
INNER JOIN Departments D ON E.DEPARTMENT_ID = D.DEPARTMENT_ID
GROUP BY D.DEPARTMENT_NAME
HAVING AVG(CAST(E.Salary AS decimal)) > 
  (SELECT AVG(CAST(Salary AS decimal)) FROM Employees)

Sample SQL Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM