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 →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| product_id | total | cum_pct |
|---|---|---|
| P2001 | 4890 | 46.7 |
| P2002 | 3215 | 77.4 |
| P2003 | 1240 | 89.3 |
| P2004 | 890 | 97.7 |
| P2005 | 320 | 100.0 |
Step by step
- Run the query without SUM and ROUND so you can see what the unfiltered values look like.
SELECT product_id, price FROM products; - 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; - 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
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 DESC2. 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 DESC3. 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 DESC4. 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_pct5. 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 <= 75Learn 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 →