Consider this table (from http://www.tizag.com/mysqlTutorial/mysqlmax.php):
Id name type price
123451 Park's Great Hits Music 19.99
123452 Silly Puddy Toy 3.99
123453 Playstation Toy 89.95
123454 Men's T-Shirt Clothing 32.50
123455 Blouse Clothing 34.97
123456 Electronica 2002 Music 3.99
123457 Country Tunes Music 21.55
123458 Watermelon Food 8.73
This SQL query returns the most expensive item from each type: SELECT type, MAX(price) FROM products GROUP BY type
Clothing $34.97
Food $8.73
Music $21.55
Toy $89.95
I also want to get the fields id and name that belong to the above max price, for each row. What SQL query will return a table like this?
Id name type price
123455 Blouse Clothing 34.97
123458 Watermelon Food 8.73
123457 Country Tunes Music 21.55
123453 Playstation Toy 89.95
This is the
greatest-n-per-group
problem that comes up frequently. My usual way of solving it is logically equivalent to the answer given by @Martin Smith, but does not use a subquery:My solution and all others given on this thread so far have a chance of producing multiple rows per value of
type
, if more than one product shares the same type and both have an equal price that is the max. There are ways to resolve this and break the tie, but you need to tell us which product "wins" in case like that.You need some other attribute that is guaranteed to be unique over all rows, at least for rows with the same
type
. For example, if the product with the greaterId
value should win, you can resolve the tie this way:Edit Just updating mine to meet the clarified requirement
You can do it with a subselect
or an inner join