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 > Selecting things greater than the max of something else

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-19-08, 19:16
_89 _89 is offline
Registered User
 
Join Date: May 2008
Posts: 6
Selecting things greater than the max of something else

Let's pretend I have this table

PHP Code:
id|name|price|type|
------------------- 
I need to select the name of all of the items that have a price greater than the max price of all items of a certain type.

So, if I have 20 items in the table and 4 of them are of type 'A' and the max price for any item of type 'A' is 10, I need to select all items of the 20 with a price greater than 10.

I tried joining the table with itself, but that didn't work so well. Can anyone tell me how to do this?

Thank you.
Reply With Quote
  #2 (permalink)  
Old 10-19-08, 21:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
this is a classic homework assignment, isn't it?

okay, can you write the query to find the largest price for type A items?

show me that, and i'll show you how to use it in a subquery
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-19-08, 21:12
_89 _89 is offline
Registered User
 
Join Date: May 2008
Posts: 6
SELECT MAX(price) AS maxPrice FROM table WHERE type = 'A';
Reply With Quote
  #4 (permalink)  
Old 10-20-08, 00:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
okay, great

now here's how to return the names of all items that have a higher price --

Code:
SELECT name
  FROM table
 WHERE price >
       ( SELECT MAX(price) AS maxPrice
           FROM table
          WHERE type = 'A' )
make sense?

note that the maxPrice alias isn't actually needed
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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