I have the relation instructor(ID, name, dept_name, salary).
How would I go about finding the name of the department with the highest average salary?
I have the relation instructor(ID, name, dept_name, salary).
How would I go about finding the name of the department with the highest average salary?
will this do the trick?
select top 1 id, name, avg (salary)
from instructor
group by id, name
order by avg (salary) desc
Given the homework tag, I won't spell it out for you, but you want to look into the AVG
function and the GROUP BY
clause.
select top 1 dept_name, avg(salary) as AvgSalary
from instructor
group by dept_name
order by AvgSalary desc
This will get you both if two deparments have the same average salary, use rownum=1 if this is not needed.
with averages as (select dept_name,avg(salary) aver from instructor group by dept_name)
select dept_name
from averages
where aver = (select max(aver) from averages)