I'm supposed to get every departments average wage and only show the department with the highest average wage. I figured out this query, but it doesn't work. Anyone got some ideas?
SELECT department, max(avg(wage))
FROM employees
GROUP BY department;
I get this error: ERROR at line 1: ORA-00937: not a single-group group function
Without CTEs you can do:
Select Z.Department, Z.AvgWage
From (
Select Department, Avg(Wage) AvgWage
From Employees
Group By Department
) As Z
Where AvgWage = (
Select Max(Z1.AvgWage)
From (
Select Department, Avg(Wage) AvgWage
From Employees
Group By Department
) Z1
)
With CTEs you could do:
With AvgWages As
(
Select Department
, Avg(Wage) AvgWage
, Rank() Over( Order By Avg(Wage) Desc ) WageRank
From Employees
Group By Department
)
Select Department, AvgWage, WageRank
From AvgWages
Where WageRank = 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With