• 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:

SQL query help

 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have table with columns as name, deptno and salary.

I need to get the names which has maximum salary in their deptmenets.

I could able to find maximum salaries in each department using

select depno, max(sal) from emp GROUP BY deptno;

But how to get correspodning names?

Thanks
Pawan
SCJP 5.0, SCWCD 1.4
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add "name" to the fields to be selected?

[edit]Add: That would probably work for a simple query, but you might need nested "selects" for anything more complicated.[/edit]
[ September 17, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which Database are you using ?

you can try this query, I haven't tested it

select name, depno, sal from emp where (depno, sal) in (
select depno, max(sal) from emp GROUP BY deptno)


Shailesh
[ September 17, 2008: Message edited by: Shailesh Chandra ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic