← Back to SQL, page 25

sql-0487 · Hard senior

Show the top three products by revenue and group the rest into an 'Other' row.

Rank products by revenue with a window function, then group again using CASE WHEN rank <= 3 THEN product ELSE 'Other' END as the label. Summing in the second step gives the top products individually and one combined row for the long tail.

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);
INSERT INTO order_items VALUES (105, 'Mug', 2, 6), (104, 'Card', 3, 2);
sql
WITH revenue AS (
  SELECT product, SUM(qty * unit_price) AS revenue FROM order_items GROUP BY product
), ranked AS (
  SELECT product, revenue, RANK() OVER (ORDER BY revenue DESC) AS rnk FROM revenue
)
SELECT CASE WHEN rnk <= 3 THEN product ELSE 'Other' END AS label, SUM(revenue) AS revenue
FROM ranked GROUP BY label ORDER BY MIN(rnk);
labelrevenue
Bag60
Lamp25
Notebook20
Other35

Bag (60), Lamp (25) and Notebook (20) are the top three, and Pen (17), Mug (12) and Card (6) fall into 'Other' with 35. Ordering by MIN(rnk) keeps the top products in rank order and puts 'Other' last. RANK means ties at third place would all be kept as separate rows; switch to ROW_NUMBER with a tiebreaker for exactly three. The totals across all rows still equal total revenue, which is a useful check.

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);
INSERT INTO order_items VALUES (105, 'Mug', 2, 6), (104, 'Card', 3, 2);

WITH revenue AS (
  SELECT product, SUM(qty * unit_price) AS revenue FROM order_items GROUP BY product
), ranked AS (
  SELECT product, revenue, RANK() OVER (ORDER BY revenue DESC) AS rnk FROM revenue
)
SELECT CASE WHEN rnk <= 3 THEN product ELSE 'Other' END AS label, SUM(revenue) AS revenue
FROM ranked GROUP BY label ORDER BY MIN(rnk);

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Rank, then relabel the tail
  • Group again for the Other row
  • Order by the best rank in each label
  • Totals should match overall revenue

Common mistakes

  • Using LIMIT 3 and losing the remainder
  • Not deciding how ties at the cut-off behave

Follow-ups

  • How would you make the cut-off a percentage of revenue?
  • How would you do this per month?

Tips

  • Check that totals still add up