Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing a Query in mysql

Tags:

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?

like image 565
Julia Avatar asked Feb 23 '26 01:02

Julia


1 Answers

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;
like image 145
Alon Eitan Avatar answered Feb 25 '26 21:02

Alon Eitan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!