Pareto Cumulative Revenue Share by Product

Quick Overview

Write a SQL query that computes each product's revenue, its share of total revenue, and the running cumulative share in descending revenue order, the table behind a Pareto chart. It tests aggregating before windowing, deterministic tie-breaking in running totals, zero-total handling, and rounding percentages correctly.

Pareto Cumulative Revenue Share by Product

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

For each product with at least one sale, compute total `revenue` (the sum of `quantity * unit_price`), its share of the grand total `revenue_pct`, and the running cumulative share `cumulative_pct`. Return `product_id`, `product_name`, `revenue`, `revenue_pct` and `cumulative_pct`. ```hint Make the running order unique Decide what fixes the order of the running total when two products earn the same revenue. ``` ### Input Table(s) Table `products` (primary key `product_id`, all columns NOT NULL): | Column | Type | Meaning | |---|---|---| | product_id | INT | Identifier of the product | | product_name | VARCHAR(100) | Name of the product | Table `sales` (primary key `sale_id`, all columns NOT NULL): | Column | Type | Meaning | |---|---|---| | sale_id | INT | Identifier of the sale | | product_id | INT | Product sold; every value exists in `products` | | quantity | INT | Units sold; at least 0 | | unit_price | DECIMAL(10,2) | Price per unit; at least 0 | ### Output Contract One row per product that has at least one row in `sales`, with these columns in this order: | Column | Definition | |---|---| | product_id | The product's `product_id` | | product_name | The product's `product_name` | | revenue | Sum of `quantity * unit_price` over the product's sales, rounded to 2 decimals | | revenue_pct | 100 times the product's revenue divided by the total revenue of all products, rounded to 2 decimals | | cumulative_pct | 100 times the running sum of revenue up to and including this row, in output order, divided by the total revenue, rounded to 2 decimals | Sort by `revenue` descending, then `product_id` ascending. `cumulative_pct` accumulates in this order and is computed from unrounded revenue. ### Example `products`: | product_id | product_name | |---|---| | 1 | Alpha | | 2 | Beta | `sales`: | sale_id | product_id | quantity | unit_price | |---|---|---|---| | 1 | 1 | 5 | 10.00 | | 2 | 2 | 5 | 10.00 | Expected output: | product_id | product_name | revenue | revenue_pct | cumulative_pct | |---|---|---|---|---| | 1 | Alpha | 50.00 | 50.00 | 50.00 | | 2 | Beta | 50.00 | 50.00 | 100.00 | Both products have revenue 50 and the grand total is 100, so each contributes 50%. The tiebreaker `product_id` ascending puts Alpha (id 1) first with a cumulative 50%, then Beta with a cumulative 100%. A second input with uneven revenue and a product that never sold: `products`: | product_id | product_name | |---|---| | 1 | Alpha | | 2 | Beta | | 3 | Gamma | | 4 | Delta | `sales`: | sale_id | product_id | quantity | unit_price | |---|---|---|---| | 1 | 1 | 3 | 10.00 | | 2 | 2 | 1 | 50.00 | | 3 | 1 | 2 | 5.00 | | 4 | 3 | 1 | 20.00 | Expected output: | product_id | product_name | revenue | revenue_pct | cumulative_pct | |---|---|---|---|---| | 2 | Beta | 50.00 | 45.45 | 45.45 | | 1 | Alpha | 40.00 | 36.36 | 81.82 | | 3 | Gamma | 20.00 | 18.18 | 100.00 | Alpha earns 30 + 10 = 40, Beta 50 and Gamma 20, for a total of 110. Delta has no sales and is left out. Alpha's cumulative share is 90 / 110 = 81.818..., which rounds to 81.82, even though 45.45 + 36.36 is 81.81. ### Constraints and Clarifications - There are 1 to 10,000 products and 0 to 100,000 sales. With no sales the result is empty. - `quantity` and `unit_price` are at least 0, so a product can have sales and a revenue of 0. Such a product is still returned. - If the grand total revenue is 0, return `0.00` for both `revenue_pct` and `cumulative_pct` on every row. - A product with several sales gets one row; its sales are summed. - Ties on revenue are broken by `product_id` ascending, both for the output order and for the running total.

Overview: Write a SQL query that computes each product's revenue, its share of total revenue, and the running cumulative share in descending revenue order, the table behind a Pareto chart. It tests aggregating before windowing, deterministic tie-breaking in running totals, zero-total handling, and rounding percentages correctly.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

For each product with at least one sale, compute total revenue (the sum of quantity * unit_price), its share of the grand total revenue_pct, and the running cumulative share cumulative_pct. Return product_id, product_name, revenue, revenue_pct and cumulative_pct.

Input Table(s)

Table products (primary key product_id, all columns NOT NULL):

ColumnTypeMeaning
product_idINTIdentifier of the product
product_nameVARCHAR(100)Name of the product

Table sales (primary key sale_id, all columns NOT NULL):

ColumnTypeMeaning
sale_idINTIdentifier of the sale
product_idINTProduct sold; every value exists in products
quantityINTUnits sold; at least 0
unit_priceDECIMAL(10,2)Price per unit; at least 0

Output Contract

One row per product that has at least one row in sales, with these columns in this order:

ColumnDefinition
product_idThe product's product_id
product_nameThe product's product_name
revenueSum of quantity * unit_price over the product's sales, rounded to 2 decimals
revenue_pct100 times the product's revenue divided by the total revenue of all products, rounded to 2 decimals
cumulative_pct100 times the running sum of revenue up to and including this row, in output order, divided by the total revenue, rounded to 2 decimals

Sort by revenue descending, then product_id ascending. cumulative_pct accumulates in this order and is computed from unrounded revenue.

Example

products:

product_idproduct_name
1Alpha
2Beta

sales:

sale_idproduct_idquantityunit_price
11510.00
22510.00

Expected output:

product_idproduct_namerevenuerevenue_pctcumulative_pct
1Alpha50.0050.0050.00
2Beta50.0050.00100.00

Both products have revenue 50 and the grand total is 100, so each contributes 50%. The tiebreaker product_id ascending puts Alpha (id 1) first with a cumulative 50%, then Beta with a cumulative 100%.

A second input with uneven revenue and a product that never sold:

products:

product_idproduct_name
1Alpha
2Beta
3Gamma
4Delta

sales:

sale_idproduct_idquantityunit_price
11310.00
22150.00
3125.00
43120.00

Expected output:

product_idproduct_namerevenuerevenue_pctcumulative_pct
2Beta50.0045.4545.45
1Alpha40.0036.3681.82
3Gamma20.0018.18100.00

Alpha earns 30 + 10 = 40, Beta 50 and Gamma 20, for a total of 110. Delta has no sales and is left out. Alpha's cumulative share is 90 / 110 = 81.818..., which rounds to 81.82, even though 45.45 + 36.36 is 81.81.

Constraints and Clarifications

  • There are 1 to 10,000 products and 0 to 100,000 sales. With no sales the result is empty.
  • quantity and unit_price are at least 0, so a product can have sales and a revenue of 0. Such a product is still returned.
  • If the grand total revenue is 0, return 0.00 for both revenue_pct and cumulative_pct on every row.
  • A product with several sales gets one row; its sales are summed.
  • Ties on revenue are broken by product_id ascending, both for the output order and for the running total.