Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);sql
WITH r AS (
SELECT product, SUM(qty * unit_price) AS revenue FROM order_items GROUP BY product
), c AS (
SELECT product, revenue,
100.0 * revenue / SUM(revenue) OVER () AS pct,
100.0 * SUM(revenue) OVER (ORDER BY revenue DESC, product) / SUM(revenue) OVER () AS cum_pct
FROM r
)
SELECT product, revenue, ROUND(cum_pct, 1) AS cum_pct
FROM c WHERE cum_pct - pct < 80
ORDER BY revenue DESC, product;| product | revenue | cum_pct |
|---|---|---|
| Bag | 60 | 49.2 |
| Lamp | 25 | 69.7 |
| Notebook | 20 | 86.1 |
Total revenue is 123. Bag (60) and Lamp (25) reach 69.7 percent, so Notebook, which takes the running share to 86.1 percent, crosses the line and is included. Pen is left out. Comparing the share before the current row (cum_pct - pct) is what includes the product that crosses the threshold.
Revenue here counts every order line, including the cancelled order. A real report would join orders and exclude cancelled ones; the definition belongs in the answer.
The window ORDER BY uses product as a tie-breaker so equal revenues produce a stable cumulative order.
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
WITH r AS (
SELECT product, SUM(qty * unit_price) AS revenue FROM order_items GROUP BY product
), c AS (
SELECT product, revenue,
100.0 * revenue / SUM(revenue) OVER () AS pct,
100.0 * SUM(revenue) OVER (ORDER BY revenue DESC, product) / SUM(revenue) OVER () AS cum_pct
FROM r
)
SELECT product, revenue, ROUND(cum_pct, 1) AS cum_pct
FROM c WHERE cum_pct - pct < 80
ORDER BY revenue DESC, product;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Aggregate first, then apply windows
- Running total over revenue descending
- Divide by SUM(...) OVER () for shares
- Compare the share before the current row to include the crossing item
Common mistakes
- Cutting off at cum_pct <= 80 and excluding the product that crosses
- Leaving ties without a deterministic order
Follow-ups
- How would you show the share per category?
- How would you exclude cancelled orders?
Tips
- Explain the crossing-row condition