SQL interview question: products that make 80% of revenue

MediumRunning total

Problem

Shared data rules

For each category, find the fewest top-earning products that together bring in at least 80% of its September 2026 revenue. Also find their share of the products sold and the share of revenue they bring.

Formulas

Example

Input: orders
order_idstatuscreated_at
501delivered2026-09-05 13:00
502delivered2026-09-01 10:30
Input: order_items
order_idproduct_idcategoryquantityprice_usdRow note
50111Beverages24
50113Beverages13← tie with 14: smaller product_id
50212Beverages23
50214Beverages13
Output
categoryproductstop_productstop_products_sharetop_revenue_share
Beverages430.750.85

Revenue: 11 — $8, 12 — $6, 13 and 14 — $3 each, out of $20. Running shares 0.4, 0.7, 0.85: 3 products of 4 are enough, and they bring 0.85.

Output columns

products
the category’s products with sales in September

Notes

Solve it free

Your query runs on PostgreSQL in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions