Most expensive product in each category
Hard
Show category, name and price of the most expensive product in each category.
Tables: customers(customer_id, name, city, country, joined) · products(product_id, name, category, price) · orders(order_id, customer_id, order_date, status) · order_items(order_id, product_id, quantity, unit_price)
Your query✔ Solved
Show the expected result
| category | name | price |
|---|---|---|
| Books | Deep Learning (book) | 89.0 |
| Electronics | 27-inch monitor | 329.0 |
| Furniture | Standing desk | 549.0 |
| Grocery | Coffee beans 1 kg | 32.0 |
| Stationery | Gel pens, pack of 10 | 11.0 |
5 rows
Show a solution
WITH r AS (SELECT category, name, price, ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC) AS rn FROM products) SELECT category, name, price FROM r WHERE rn = 1;
SQL used
New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.