Chapter 8 · Window Functions
03. Rank Products by Revenue Within Category
Medium20 XPE-Commerce
For each product, calculate its total revenue from order_items (quantity * unit_price) and rank it within its category using RANK(). Return the category name, product name, total revenue (rounded to 2), and the rank. Order by category name, then rank.
Tables
- PKidINT
- nameTEXT
- descriptionTEXT
- PKidINT
- nameTEXT
- FKcategory_id→ categories.idINT
- priceREAL
- costREAL
- stock_quantityINT
- created_atTIMESTAMP
- PKidINT
- FKorder_id→ orders.idINT
- FKproduct_id→ products.idINT
- quantityINT
- unit_priceREAL
- discountREAL
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.