Julia
Julia

Reputation: 33

Fixing a Query in mysql

I need to display the code of the Department in the School table with the highest total Salary of any department.

So I tried this:

SELECT MAX(Total), dept 

FROM (SELECT SUM(salary) AS Total, dept from school group by dept) AS Temp;

Which gives me the correct result; however it shows the field MAX(Total) and I just want the code of the department to show. What should I change?

Upvotes: 3

Views: 23

Answers (2)

vp_arth
vp_arth

Reputation: 14982

If you need department code with max salary fund:

SELECT a.dept 
FROM (SELECT SUM(salary) AS Total, dept from school group by dept) AS a
ORDER BY a.Total DESC
LIMIT 1;

Upvotes: 0

Alon Eitan
Alon Eitan

Reputation: 12025

You can do

SELECT a.dept FROM (
    SELECT MAX(Total), dept 

    FROM (SELECT SUM(salary) AS Total, dept from school group by dept) AS Temp
) AS a;

Upvotes: 1

Related Questions