If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Order by - query highest 5

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-19-11, 07:05
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
Order by - query highest 5

I need to "List all details of the 5 highest prize money, descending order by money." I ran this query first:

select * from prize
order by money desc;

Then, seeing the 5th highest figure was >60, I ran this query:

select * from prize
where money > 60.00
order by money desc;

How can I combine these two into one statement?
Reply With Quote
  #2 (permalink)  
Old 03-19-11, 09:40
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
MySQL includes a nice feature called LIMIT which can be included in your SQL statement. This effectively limits the number of rows returned:

Code:
SELECT *
FROM   prize
ORDER  BY money DESC
LIMIT  5;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 03-20-11, 00:19
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
order by - query highest 5

Thanks for this, Ronan.

However, returning 5 rows isn't the correct answer because there are two records which have the same prize amount. The answer should display 6 rows, and I know LIMIT 6 would do that, but is there a statement that will return the 5 highest prizes without knowing how many records this covers? Eg, what if there were 25 records which all fall into the top 5 prize amounts?
In other words, do I need to look at the data to work out how many records should be returned in my query before being able to create this query, or can I create a query which will do it all for me?
Reply With Quote
  #4 (permalink)  
Old 03-20-11, 04:40
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Code:
SELECT this.money 
  FROM prize AS this
LEFT OUTER
  JOIN prize as that
    ON that.money > this.money
GROUP
    BY this.money
HAVING COUNT(that.money) < 5
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-20-11, 05:26
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
order by - query highest 5

Thank you.
Unfortunately this returns four rows.
Reply With Quote
  #6 (permalink)  
Old 03-20-11, 07:16
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by melgra70 View Post
Thank you.
Unfortunately this returns four rows.
yeah, you're right, i didn't have enough information

could you please help me out and do a SHOW CREATE TABLE

i will fix the query accordingly
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 03-20-11, 07:27
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
It is possible to include a LIMIT into a SELECT statement so long as it is not a subquery. So you could do this:

Code:
SELECT this.money
FROM   prize AS this,
       (SELECT DISTINCT money
        FROM   prize
        ORDER  BY money DESC
        LIMIT  5) AS that
WHERE  this.money = that.money;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #8 (permalink)  
Old 03-20-11, 08:00
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
order by - query highest 5

Many thanks, Rudi and Rowan.
Rowan's query works.
Reply With Quote
  #9 (permalink)  
Old 03-20-11, 13:29
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Actually it is Ronan but never mind glad the query works.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
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 On
Pingbacks are On
Refbacks are On