• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Query -- select maximum salary in a sum table

 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Martin, Gabriel and Sergy for you quick responses!! I can understand why Coderanch is so popular discussion forum.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic