Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query -- select maximum salary in a sum table

 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I have an employee table with following fields
lastname,departmentID,salary
now i want to write a query on this table to get me the department id which gives maximum total salary to its employees. I wrote below query.

mysql> select sum(e1.salary), e1.departmentid from employee e1 group by e1.departmentid having sum(e1.salary) > 50000;

above query generates proper result when maximum sum salary of a department is > 50000 but as i wrote above i want to generate the department which gives maximum salary (i dont want to compare with 50000)

 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd do the following:and then read just the first record returned. I assume this is possible in MySQL.

If you really do need to limit the result to one row (but why?), it should be possible too, but these mechanisms differ from database to database and I don't know the proper form for MySQL.
 
Gabriel Jay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A slight modification to the above query will return a single record:

SELECT sum(e1.salary), e1.departmentid FROM employee e1 GROUP BY e1.departmentid ORDER BY sum(e1.salary) DESC LIMIT 1;
 
Sergey Mois
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Martin, Gabriel and Sergy for you quick responses!! I can understand why Coderanch is so popular discussion forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic