runtime
27 / 88

task RANK / DENSE_RANK · medium

Bestsellers within each category

For non-cancelled orders, total the units (quantity) sold per catalog item. Return category, name, units and category_rank, where rank 1 is the item with the most units in its category.

what to return

Columns
categorynameunitscategory_rank in this order
Rows
12 rows · any order
Watch for
Tied items share a rank and the next rank is not skipped (1, 1, 2).
Items with no units sold are not listed.
Skill
Rank aggregated values per group with DENSE_RANK. Refresher: Latest row per group →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result12 rows · any order
categorynameunitscategory_rank
GardenSeed Starter Kit61
GardenPruning Shears52
GardenGarden Hose43

first 3 of 12 rows

data you can query

Sample rows from each table. Each column shows its type underneath; → marks a link to another table.

order_lines4 columns · 48 rows
order_idinteger → sales_ordersitem_idinteger → catalog_itemsquantityintegerunit_pricereal
1011139
1013219.5
1024159
1035145
10310312

showing 5 of 48 rows

sales_orders5 columns · 33 rows
idinteger · keyshopper_idinteger → shoppersorder_datetextstatustextdelivered_datetext
10112025-01-04delivered2025-01-08
10222025-01-06delivered2025-01-07
10312025-01-15delivered2025-01-20
10432025-01-18cancelledNULL
10542025-01-22delivered2025-01-31

showing 5 of 33 rows

catalog_items4 columns · 13 rows
idinteger · keynametextcategorytextpricereal
1Desk LampLighting39
2Floor lampLighting89
3String LightsLighting19.5
4Chef KnifeKitchen59
5Cast Iron PanKitchen45

showing 5 of 13 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.