Units sold per product

mediumSQL~15 min · 4 tests · bitshop

The catalogue team wants every product on one sheet, with how many units actually reached customers. Products that never sold must still be on the sheet, with a 0.

Count order_items.quantity only for orders whose status is 'delivered'. A product whose only sales were cancelled, returned or still shipping has units_sold = 0.

Return one row per product in products:

column type meaning
product_id integer the product
name text the product name
units_sold integer total delivered quantity, 0 if none

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