Top product per category

mediumSQL~20 min · 4 tests · bitshop

Each category page gets a “best seller” badge. Find which product earns it.

A product’s units are the sum of order_items.quantity over orders with status = 'delivered'. The best seller in a category is the product with the most units. If two or more products tie for the top, they all get the badge, so return all of them.

Only products with at least one delivered unit can win. A category with no delivered sales has no row.

Return one row per winning product:

column type meaning
category text the category
product_id integer the winning product
name text its name
units integer its delivered units

Row order doesn’t matter.

Tables: products, order_items, orders.

Learn the ideaRead the lesson →
solution.sqlDuckDB · bitshop

Press ⌘ ↵ or Run to check the visible tests. Submit runs the hidden ones too. Everything runs in your browser.

⌘ ↵ to run