• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Query -- select maximum salary in a sum table

 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • 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)

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • 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.
 
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;
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • 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.
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic