Cumulative share of revenue
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
Return each sold product with its revenue (quantity times price) and the cumulative share of total revenue in percent, rounded to 1 decimal, going from the product with the most revenue to the least. Order by revenue descending, then by product name. The last row shows 100.
order_items (first 12 of 18 rows)
| order_id | product_id | qty |
|---|---|---|
| 101 | 3 | 4 |
| 101 | 4 | 1 |
| 102 | 7 | 2 |
| 103 | 2 | 1 |
| 104 | 6 | 1 |
| 105 | 5 | 2 |
| 105 | 4 | 3 |
| 106 | 1 | 2 |
| 107 | 3 | 6 |
| 108 | 5 | 1 |
| 109 | 7 | 1 |
| 109 | 6 | 2 |
products (8 rows)
| id | name | category | price |
|---|---|---|---|
| 1 | Desk Lamp | Home | 35 |
| 2 | Office Chair | Home | 180 |
| 3 | Notebook | Stationery | 4.5 |
| 4 | Pen Set | Stationery | 12 |
| 5 | Headphones | Electronics | 95 |
| 6 | Webcam | Electronics | 60 |
| 7 | Mug | Home | 9 |
| 8 | Backpack | Travel | 70 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
Two window sums with different OVER clauses can sit side by side. The one with ORDER BY accumulates, the empty one covers everything. This is the query behind a Pareto or ABC analysis.
A hint if you get stuck
Revenue per product in a CTE. Then SUM(rev) OVER (ORDER BY rev DESC, name) for the running total and SUM(rev) OVER () for the grand total, in one SELECT.
Background you need
Common mistakes on this exercise
- Each product's own share, not cumulative.
- Sorted from least to most revenue.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore practice on the same topic
More exercises in Advanced
- Above the average salary
- Rank within department
- Running revenue total
- Running total per customer
- Top earner per department
- Second highest salary
- Big spenders
- Levels in the reporting chain
- Dense rank
- Change from the previous order
- Days until the next order
- Moving average
- Share of revenue
- Share of the customer's total
- Each customer's first order
- Month over month
- Top two per category
- Orders above the customer's average
- Compared to the first order
- Salary quartiles
- Latest order per customer (DISTINCT ON)
- Two largest paid orders per customer (LATERAL)
- Most orders, ties included
- Last event per user, with a tie-break
- Year-over-year growth
- Price at the time of the order
- Read fields out of JSON
- One row per array element