Chapter 7 · CTEs
07. Product Profit Margin Ranking
Hard40 XPE-Commerce
Rank products by their profit margin. Use a CTE to compute margin as (price - cost) / price * 100 for each product. Then show product name, price, cost, margin (rounded to 2 decimals), and the rank (highest margin = 1). Only include products where cost is not NULL. Sort by rank.
Tables
- PKidINT
- nameTEXT
- FKcategory_id→ categories.idINT
- priceREAL
- costREAL
- stock_quantityINT
- created_atTIMESTAMP
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.