I have a database for exercises. I would like to list out the maximum price of different types with the corresponding title name, but of course I can't just put the title name attribute into my SELECT clause, because it does not appear in the GROUP BY clause. Is there any solution to my problem? Thanks!
SELECT type, MAX(price) "price"
FROM titles
GROUP BY type
ORDER BY type DESC;
You don't mention the database you are using. Most databases support the ANSI standard
row_number()
and window/analytic functions. Here is one method to do what you want:In the case of MySQL, which does not support
row_number()
, you can do:Note that this assumes that no title contains the character
'|'
.