Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 4th, 2003, 03:56 PM
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select top third Salary

How can I select the employee who has top third salary from employee table which has Name and sal columns.

Name Sal

Dave 80000
Tom 70000
Dan 55000
John 81000
Jim 48000

Thanks
Reply With Quote
  #2 (permalink)  
Old December 4th, 2003, 04:09 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Location: Clarksville, TN, USA.
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What is the front end? You could select top 3 and then the min of top 3. Use a subquery.



Sal
Reply With Quote
  #3 (permalink)  
Old December 4th, 2003, 04:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Can't quite parse your requirement. Do you mean you want the set of rows whose 'Sal' value is in the top 1/3 of all 'Sal' values? If your database implements the TOP clause (e.g. SQL Server) you could use:
Code:
SELECT TOP 33.3 PERCENT * FROM yourtable ORDER BY Sal DESC;
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #4 (permalink)  
Old December 4th, 2003, 04:26 PM
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your quick responses.

Database is Oracle and by top third I meant that, select the name of the person who gets the 3rd highest salary from the table.


Reply With Quote
  #5 (permalink)  
Old December 4th, 2003, 06:32 PM
Authorized User
 
Join Date: Oct 2003
Location: roanoke, va, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'd suggest that You use

select rownum = 3

ORDER BY Sal DESC

Dave

Reply With Quote
  #6 (permalink)  
Old December 5th, 2003, 08:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try this:
Code:
SELECT T0.Name, T0.Sal FROM yourtable T0
WHERE 3 = (SELECT COUNT(DISTINCT Sal) FROM yourtable T1
        WHERE T1.Sal>=T0.Sal)
The (correlated) subquery assigns a ranking number to each row based on the 'Sal' column. The WHERE clause finds the row with the ranking number equal to 3. Note that this deals with ties by assigning the same ranking number to each row with the same value, so you could get more than one row in the resultset.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #7 (permalink)  
Old December 5th, 2003, 10:59 AM
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, This works like you described.

Reply With Quote
  #8 (permalink)  
Old December 18th, 2003, 05:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another way of doing it would be to use a derived table. The inner query selects the top 3 salaries and the outer query selects the lowest salary from this result set.
Code:
  SELECT TOP 1 [Name], Sal
    FROM (SELECT TOP 3 [Name], Sal
            FROM employee
        ORDER BY Sal DESC) AS E2
ORDER BY Sal
This will return only one row, and that row will be the third row in the table if you had selected it by returning all the rows, ordered by salary and moved to the third row. Jeff's query is probably closer to what you want but it shows that there are usually more than one way do achieving what you want in SQL.

Regards
Owain Williams
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT TOP n rgerald SQL Server 2000 3 May 12th, 2006 04:03 PM
SELECT TOP n NOT SELECTING TOP n! ibi SQL Language 8 March 30th, 2005 08:08 PM
SELECT TOP FROM HAVING khatfield29 SQL Language 1 August 23rd, 2004 02:41 PM
SELECT TOP !!!! Jane SQL Language 2 October 17th, 2003 11:22 AM
SELECT TOP ciko1973 SQL Language 5 August 8th, 2003 04:35 AM



All times are GMT -4. The time now is 08:49 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.