Product Revenue Share
The merchandising team at Whetcode Supply suspects the shop is really a one-product business wearing a five-product catalogue, and wants the evidence: each item's share of everything the shop has taken, with the three biggest shares on the page. A share, not an amount — the amounts are already known and they do not settle the argument.
That is the awkward part. Each line needs a figure for its own item and a figure covering every item at once, and the second one is out of view once the rows have been gathered item by item.
orders — one row per order. customer_id is the id of the customer who placed it, product_id the id of the item bought, quantity how many units of that item, and order_date the day it was placed. An order row covers one item only.
| id | customer_id | product_id | quantity | order_date |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | 2023-02-01 |
| 2 | 1 | 4 | 1 | 2023-02-01 |
| 3 | 2 | 2 | 1 | 2023-02-10 |
| 4 | 3 | 3 | 3 | 2023-03-05 |
| 5 | 4 | 5 | 5 | 2023-03-11 |
| 6 | 1 | 3 | 1 | 2023-04-02 |
| 7 | 5 | 1 | 4 | 2023-05-20 |
| 8 | 2 | 5 | 2 | 2023-05-22 |
| 9 | 3 | 4 | 1 | 2023-06-01 |
| 10 | 5 | 2 | 1 | 2023-06-15 |
products — the catalogue, one row per item on sale. price is what a single unit costs, in dollars.
| id | name | category | price |
|---|---|---|---|
| 1 | Wireless Mouse | electronics | 25.0 |
| 2 | Standing Desk | furniture | 350.0 |
| 3 | Desk Lamp | furniture | 40.0 |
| 4 | Mechanical Keyboard | electronics | 85.0 |
| 5 | Notebook Set | office | 12.0 |
The tables already exist in the database — there is nothing to create or load.
Task: Write a query returning two columns, name (the product's name) and revenue_pct (that product's revenue as a percentage of the shop's total revenue, rounded to one decimal place), returning only the three largest shares, biggest first. Give a percentage out of 100, not a fraction of 1 — a product holding a tenth of revenue reads 10.0, not 0.1. The third and fourth shares are almost a point apart, so which three come back is not in doubt.
Example output — shape only, on an invented catalogue. The figures are percentages out of 100: an item on 41.8 holds a little over two fifths of the takings:
| name | revenue_pct |
|---|---|
| Monitor Riser | 41.8 |
| Cable Tidy | 22.3 |
| Foot Rest | 16.9 |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in