Home > Ask the Oracle Database / Applications Experts > Questions & Answers > How to find the first 5 highest salaried employees in each department
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

How to find the first 5 highest salaried employees in each department

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 25 April 2001

Given employee table in scott user, how will I find the first 5 highest salaried employees in each dept.?


>

Assuming this is SQL/Server 7, use the SELECT TOP option to select the top 5 in each department in a correlated subselect --

select *
  from scott.employee X
 where salary in
       (select top 5 salary
          from scott.employee
         where dept = X.dept
       order by salary desc)
 order by dept, salary desc

Sorry, I'm not 100% sure this will work because I do not have access to SQL/Server to test this. I'm not certain the syntax for referring to scott's employee table is correct.

Note that the ORDER BY in the subselect is descending, so that the TOP function will pick the five highest salaries. The ORDER BY in the outer query sorts the results by highest salaries within department.

By the way, both I and Craig Mullins, another of searchDatabase.com's database experts, have written web page articles which discuss the "Top Ten" problem --

The "Top Ten" Problem by Craig S. Mullins
http://www.craigmullins.com /ssu_0900.htm

Top Ten SQL by r937
http://evolt.org /article/TopTenSQL/18/131/evolt.org

Mine's a little older and does not discuss SELECT TOP.

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts