GeeCON Prague 2014*
The moose likes JDBC and the fly likes Query -- select maximum salary in a sum table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Query -- select maximum salary in a sum table" Watch "Query -- select maximum salary in a sum table" New topic
Author

Query -- select maximum salary in a sum table

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

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)


thanks
Tanu Gulati
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Jun 28, 2010
Posts: 2
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

Joined: Aug 25, 2009
Posts: 5
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

thanks Martin, Gabriel and Sergy for you quick responses!! I can understand why Coderanch is so popular discussion forum.
 
GeeCON Prague 2014
 
subject: Query -- select maximum salary in a sum table