BBampaa

Lessons › SQL › Go further in SQL: Business Analytics Patterns

SQL lesson

Pareto 80-20 cumulative analysis

Pareto 80-20 analysis ranks items from largest contributor to smallest and tracks a running total until you hit 80% of the whole - revealing the small group responsible for most of the result. Like scanning a playlist sorted by most-played: you stop once the top few songs account for 80% of all listens, because those are the ones worth your attention.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Pareto analysis - finding the 80%

SELECT product_id,
       SUM(price) AS total,
       ROUND(100.0 * SUM(SUM(price)) OVER (ORDER BY SUM(price) DESC) / SUM(SUM(price)) OVER (), 1) AS cum_pct
FROM products
GROUP BY product_id
ORDER BY total DESC

Result:

product_idtotalcum_pct
P2001489046.7
P2002321577.4
P2003124089.3
P200489097.7
P2005320100.0

Step by step

  1. Run the query without SUM and ROUND so you can see what the unfiltered values look like.
    SELECT product_id, price FROM products;
  2. SUM totals the numbers in a column. Works on numeric columns only. ROUND rounds the value to the nearest integer (or to N decimal places with a second argument).
    SELECT product_id, SUM(price), ROUND(100.0 * SUM(SUM(price)) OVER (ORDER BY SUM(price) DESC) / SUM(SUM(price)) OVER (), 1) FROM products GROUP BY product_id ORDER BY SUM(price) DESC;
  3. AS gives the SUM and ROUND results friendly column names so the output reads cleanly.
    SELECT product_id, SUM(price) AS total, ROUND(100.0 * SUM(SUM(price)) OVER (ORDER BY SUM(price) DESC) / SUM(SUM(price)) OVER (), 1) AS cum_pct FROM products GROUP BY product_id ORDER BY total DESC;

Practice

Try each one in your head first, then open the answer.

1. Using product_sales, show each product's share of total revenue as pct_of_total, rounded to 1 decimal, biggest seller first. Show product and pct_of_total.
SELECT product, ROUND(100.0 * revenue / SUM(revenue) OVER (), 1) AS pct_of_total FROM product_sales ORDER BY revenue DESC
2. Using product_sales, show product, revenue and running_revenue: a running total of revenue from the biggest seller down.
SELECT product, revenue, SUM(revenue) OVER (ORDER BY revenue DESC) AS running_revenue FROM product_sales ORDER BY revenue DESC
3. Using product_sales, show product, revenue and cum_pct: the running percent of total revenue from the biggest seller down, rounded to 1 decimal.
SELECT product, revenue, ROUND(100.0 * SUM(revenue) OVER (ORDER BY revenue DESC) / SUM(revenue) OVER (), 1) AS cum_pct FROM product_sales ORDER BY revenue DESC
4. Using product_sales, which products bring in the first 80% of revenue? Show product and cum_pct (the running percent, biggest seller first, 1 decimal) for products whose cum_pct is 80 or less.
WITH c AS (SELECT product, ROUND(100.0 * SUM(revenue) OVER (ORDER BY revenue DESC) / SUM(revenue) OVER (), 1) AS cum_pct FROM product_sales) SELECT product, cum_pct FROM c WHERE cum_pct <= 80 ORDER BY cum_pct
5. Using customer_sales, how many customers make up the first 75% of revenue (biggest customer first)? Name the column customers.
WITH c AS (SELECT customer_id, 100.0 * SUM(revenue) OVER (ORDER BY revenue DESC) / SUM(revenue) OVER () AS cum_pct FROM customer_sales) SELECT COUNT(*) AS customers FROM c WHERE cum_pct <= 75

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed