Priciest Item in Each Category

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
Amazon

Problem

When two products share a category's top price, neither is more expensive than the other, so the merchandiser building the 'priciest in the aisle' strip wants every product at that price shown. From the products table, list each category's priciest products with their price, categories alphabetically and the products within a category alphabetically by name.

The most expensive item per category.

Tables

products

Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock INTEGER

products: 10 of 200 rows
product_idproduct_namecategorypriceratingin_stock
1001Basic Device 1XBooks17.522.71
1050Deluxe Bundle 2XClothing25.054.41
1099Pro Unit 3XHome & Kitchen32.582.11
1148Ultra Tool 4XCollectibles40.113.81
1197Essential Set 5XToys47.641.51
1246Classic System 6XBeauty03.21
1295Smart Gadget 7XAutomotive62.74.90
1344Eco Kit 8XGarden70.232.61
1393Turbo Pack 9XMusic77.764.30
1442Premium Widget 10XElectronics85.2921

Expected output

First 5 of 11 rows
categoryproduct_nameprice
AutomotiveEco Widget v971831.91
BeautySmart Set v961822.64
BooksDeluxe Widget v911776.29
ClothingPro Set v921785.56
CollectiblesUltra Tool 14X115.41

Worked solution and explanation

What this really is

This is a per-group top-1 dressed up as a merchandising strip, and what separates candidates is what happens at the top of each aisle. Anyone can find the highest price. The skill is returning the product that carries it, once per category, without quietly choosing one product when two share that price. Compute the top over the whole catalog and you get one row, Electronics at 1859.72, instead of eleven. Number the rows per category and keep the first, and your answer matches today's catalog by luck, then drops a tied product the first day two items share a category's top price.

Step 1: Scope the top price to each category

The question is per aisle, so the comparison has to restart in every category. PARTITION BY category does that: Collectibles, with its two products, is compared only against itself. That is why its winner is 'Ultra Tool 14X' at 115.41 while every other aisle tops out above 1,700.

Step 2: Give tied prices the same position

DENSE_RANK() OVER (PARTITION BY category ORDER BY price DESC) gives position 1 to every product at a category's top price. That is the whole tie rule in one function: the ranking keeps the promise in the statement, not a later cleanup.

Step 3: Keep position 1, then sort for the shopper

Filter rnk = 1 in the outer query, because a window result cannot be filtered in the same SELECT that computes it. Then sort by category and product_name, the order the page shows. Both keys are output columns, so a tied pair lands alphabetically with no extra tiebreaker.

Every top-priced product per category
SELECT category, product_name, price
FROM (
    SELECT
        category,
        product_name,
        price,
        DENSE_RANK() OVER (PARTITION BY category ORDER BY price DESC) AS rnk
    FROM products
) ranked
WHERE rnk = 1
ORDER BY category, product_name

DENSE_RANK keeps all tied winners; the outer query filters to position 1 and sorts for browsing.

One row per category
ROW_NUMBER() gives tied products positions 1 and 2 in whatever order the engine reads them. rn = 1 keeps one of them, and which one can change between runs.
Every product at the top price
DENSE_RANK() gives both position 1, so rnk = 1 keeps both. On this catalog the two queries return the same 11 rows; only this one stays correct when a price ties.

Common follow-up questions