“80% of our revenue comes from 20% of our customers.” Managers love saying it, but is it true for your business? And which customers are in that 20%?
That’s Pareto analysis, often called the 80/20 rule, and it’s one of the most useful reports you can build with SQL. It tells you which customers (or products, or suppliers) matter most, so sales teams know who to look after and buyers know which items must never run out. This guide builds it step by step with window functions, then turns it into the classic ABC classification.

Table of Contents
The sample data
Total revenue per customer for the year. In a real system you’d calculate this from the orders table first (shown at the end); here it’s ready-made so we can focus on the analysis:
CREATE TABLE CustomerSales (
CustomerID INT PRIMARY KEY,
Name VARCHAR(30),
Revenue DECIMAL(10,2)
);
INSERT INTO CustomerSales VALUES
(1, 'Al Noor Trading', 42000), (2, 'Gulf Office Supplies', 31000),
(3, 'Riffa Mart', 18000), (4, 'Bahrain Tech', 9000),
(5, 'Seef Stationery', 6000), (6, 'Manama Cafe', 4500),
(7, 'Isa Town Bakery', 3500), (8, 'Hidd Garage', 2500),
(9, 'Zallaq Resort', 2000), (10, 'Budaiya Farm', 1500);
Ten customers, 120,000 in total revenue.
Step 1: Sort by revenue and add a running total
The idea of Pareto analysis is simple: line the customers up from biggest to smallest, then keep a running total. A running total is a SUM() with a window that grows one row at a time:
SELECT CustomerID, Name, Revenue,
SUM(Revenue) OVER (ORDER BY Revenue DESC, CustomerID
ROWS UNBOUNDED PRECEDING) AS RunningRevenue
FROM CustomerSales
ORDER BY Revenue DESC, CustomerID;
ORDER BY Revenue DESC inside the window puts the biggest customer first. CustomerID is a tie-breaker, so two customers with equal revenue are always in the same order. ROWS UNBOUNDED PRECEDING means “from the first row up to this one”.
Step 2: Turn it into percentages
Divide the running total by the grand total. SUM(Revenue) OVER (), a window with no ORDER BY, is the grand total repeated on every row. Do the same for the customers: row number divided by the number of customers.
SELECT CustomerID, Name, Revenue,
SUM(Revenue) OVER (ORDER BY Revenue DESC, CustomerID
ROWS UNBOUNDED PRECEDING) AS RunningRevenue,
ROUND(100.0 * SUM(Revenue) OVER (ORDER BY Revenue DESC, CustomerID
ROWS UNBOUNDED PRECEDING)
/ SUM(Revenue) OVER (), 1) AS RunningPct,
ROUND(100.0 * ROW_NUMBER() OVER (ORDER BY Revenue DESC, CustomerID)
/ COUNT(*) OVER (), 1) AS CustomerPct
FROM CustomerSales
ORDER BY Revenue DESC, CustomerID;
| CustomerID | Name | Revenue | RunningRevenue | RunningPct | CustomerPct |
|---|---|---|---|---|---|
| 1 | Al Noor Trading | 42000.00 | 42000.00 | 35.0 | 10.0 |
| 2 | Gulf Office Supplies | 31000.00 | 73000.00 | 60.8 | 20.0 |
| 3 | Riffa Mart | 18000.00 | 91000.00 | 75.8 | 30.0 |
| 4 | Bahrain Tech | 9000.00 | 100000.00 | 83.3 | 40.0 |
| 5 | Seef Stationery | 6000.00 | 106000.00 | 88.3 | 50.0 |
| 6 | Manama Cafe | 4500.00 | 110500.00 | 92.1 | 60.0 |
| 7 | Isa Town Bakery | 3500.00 | 114000.00 | 95.0 | 70.0 |
| 8 | Hidd Garage | 2500.00 | 116500.00 | 97.1 | 80.0 |
| 9 | Zallaq Resort | 2000.00 | 118500.00 | 98.8 | 90.0 |
| 10 | Budaiya Farm | 1500.00 | 120000.00 | 100.0 | 100.0 |
Read it across a row: the top 20% of customers (the first two) bring in 60.8% of revenue. The top 40% bring in 83.3%. So here it’s not exactly 80/20; it’s closer to “40% of customers make 80% of revenue”. That’s normal: 80/20 is a rule of thumb, not a law, and the real split is precisely what this report tells you.
Note the 100.0 instead of 100: in SQL Server, dividing whole numbers gives a whole number, so 100 * 1 / 10 would round down. The decimal forces a decimal result.
Step 3: ABC classification
The running percentage lets you put each customer into a class:
- A: the customers that together make up the first 80% of revenue. Your most important accounts.
- B: the next 15% (up to 95%).
- C: the last 5%: many small customers.
There’s one subtlety: which side of the line does the customer that crosses 80% belong to? Bahrain Tech takes the running total from 75.8% to 83.3%. Most people count it as A, because before it was added, the A group wasn’t yet at 80%. So the rule is based on the running percentage before each customer:
WITH Ranked AS (
SELECT CustomerID, Name, Revenue,
100.0 * SUM(Revenue) OVER (ORDER BY Revenue DESC, CustomerID
ROWS UNBOUNDED PRECEDING)
/ SUM(Revenue) OVER () AS RunningPct,
100.0 * (SUM(Revenue) OVER (ORDER BY Revenue DESC, CustomerID
ROWS UNBOUNDED PRECEDING) - Revenue)
/ SUM(Revenue) OVER () AS PctBefore
FROM CustomerSales
)
SELECT CustomerID, Name, Revenue,
ROUND(RunningPct, 1) AS RunningPct,
CASE WHEN PctBefore < 80 THEN 'A'
WHEN PctBefore < 95 THEN 'B'
ELSE 'C'
END AS Class
FROM Ranked
ORDER BY Revenue DESC;
| CustomerID | Name | Revenue | RunningPct | Class |
|---|---|---|---|---|
| 1 | Al Noor Trading | 42000.00 | 35.0 | A |
| 2 | Gulf Office Supplies | 31000.00 | 60.8 | A |
| 3 | Riffa Mart | 18000.00 | 75.8 | A |
| 4 | Bahrain Tech | 9000.00 | 83.3 | A |
| 5 | Seef Stationery | 6000.00 | 88.3 | B |
| 6 | Manama Cafe | 4500.00 | 92.1 | B |
| 7 | Isa Town Bakery | 3500.00 | 95.0 | B |
| 8 | Hidd Garage | 2500.00 | 97.1 | C |
| 9 | Zallaq Resort | 2000.00 | 98.8 | C |
| 10 | Budaiya Farm | 1500.00 | 100.0 | C |
PctBefore is the running total minus the current row’s revenue, as a percentage. The CASE expression does the rest. The 80/95 limits are a common choice; change them to suit your business.
Summarize the classes
Wrap it once more to see how the classes compare:
WITH Ranked AS (
SELECT Revenue,
100.0 * (SUM(Revenue) OVER (ORDER BY Revenue DESC, CustomerID
ROWS UNBOUNDED PRECEDING) - Revenue)
/ SUM(Revenue) OVER () AS PctBefore
FROM CustomerSales
),
Classified AS (
SELECT CASE WHEN PctBefore < 80 THEN 'A'
WHEN PctBefore < 95 THEN 'B'
ELSE 'C' END AS Class,
Revenue
FROM Ranked
)
SELECT Class,
COUNT(*) AS Customers,
SUM(Revenue) AS Revenue,
ROUND(100.0 * SUM(Revenue) / (SELECT SUM(Revenue) FROM CustomerSales), 1) AS PctRevenue
FROM Classified
GROUP BY Class
ORDER BY Class;
| Class | Customers | Revenue | PctRevenue |
|---|---|---|---|
| A | 4 | 100000.00 | 83.3 |
| B | 3 | 14000.00 | 11.7 |
| C | 3 | 6000.00 | 5.0 |
Four customers out of ten bring in 83% of revenue. That’s the one-line summary a manager wants.
Another view: revenue by customer quintile
Sometimes the question is the reverse: “how much do the top 20% of customers bring in?” NTILE(5) splits the sorted customers into five equal groups (quintiles):
WITH q AS (
SELECT Revenue, NTILE(5) OVER (ORDER BY Revenue DESC, CustomerID) AS Quintile
FROM CustomerSales
)
SELECT Quintile,
COUNT(*) AS Customers,
SUM(Revenue) AS Revenue,
ROUND(100.0 * SUM(Revenue) / (SELECT SUM(Revenue) FROM CustomerSales), 1) AS PctRevenue
FROM q
GROUP BY Quintile
ORDER BY Quintile;
| Quintile | Customers | Revenue | PctRevenue |
|---|---|---|---|
| 1 | 2 | 73000.00 | 60.8 |
| 2 | 2 | 27000.00 | 22.5 |
| 3 | 2 | 10500.00 | 8.8 |
| 4 | 2 | 6000.00 | 5.0 |
| 5 | 2 | 3500.00 | 2.9 |
The top fifth brings in 60.8% of revenue; the bottom fifth, 2.9%. With thousands of customers, use NTILE(10) (deciles) or NTILE(100) for a finer picture.
Starting from real orders
In practice, the revenue per customer comes from an orders table for a chosen period. Put that in a CTE and run the same analysis on top of it:
WITH CustomerSales AS (
SELECT o.CustomerID, c.Name, SUM(o.Amount) AS Revenue
FROM dbo.Orders AS o
JOIN dbo.Customers AS c ON c.CustomerID = o.CustomerID
WHERE o.OrderDate >= '2026-01-01' AND o.OrderDate < '2027-01-01'
AND o.Status <> 'Cancelled'
GROUP BY o.CustomerID, c.Name
)
SELECT CustomerID, Name, Revenue,
ROUND(100.0 * SUM(Revenue) OVER (ORDER BY Revenue DESC, CustomerID
ROWS UNBOUNDED PRECEDING)
/ SUM(Revenue) OVER (), 1) AS RunningPct
FROM CustomerSales
ORDER BY Revenue DESC;
Decide carefully what counts as revenue: exclude cancelled orders and returns, and decide whether to use amounts before or after VAT and discounts. Use a date range rather than YEAR(OrderDate) = 2026 so an index on OrderDate can be used.
Beyond customers
The same query works for any “few big, many small” question. Just change what you group by:
- Products: which items make 80% of sales? A items should never be out of stock; C items might be candidates for removal. This is classic ABC inventory analysis.
- Suppliers: where most of your spending goes, and where negotiation pays off.
- Support tickets by category: which few problems cause most of the calls.
- Expense accounts in a general ledger: which accounts make up most of the costs.
Common mistakes
- No tie-breaker in the window’s
ORDER BY. Customers with equal revenue can swap places between runs, and with the defaultRANGEframe they’d even share one running total. Add a unique column andROWS UNBOUNDED PRECEDING. - Integer division in percentages. Multiply by
100.0. - Including cancelled orders or returns in revenue.
- Treating 80/20 as a fact rather than measuring the real split.
- Comparing periods of different lengths, like this year so far against all of last year.
- Forgetting customers with no revenue. If you need them in the list (as “class C, zero”), start from the customers table with a
LEFT JOIN.
Practice exercises
Exercise 1. Using CustomerSales, list only the customers in class A.
Exercise 2. What percentage of revenue comes from the top 3 customers?
Exercise 3. Change the classification so that A is the first 70% of revenue and B the next 20%. Which customers change class?
Answers
Answer 1. Reuse the Ranked CTE from Step 3 and filter: ... SELECT CustomerID, Name, Revenue FROM Ranked WHERE PctBefore < 80;. The result: Al Noor Trading, Gulf Office Supplies, Riffa Mart and Bahrain Tech.
Answer 2. The running percentage on the third row: 75.8% (91,000 out of 120,000). As a query:
SELECT ROUND(100.0 * SUM(Revenue) / (SELECT SUM(Revenue) FROM CustomerSales), 1) AS Top3Pct
FROM (SELECT TOP (3) Revenue FROM CustomerSales ORDER BY Revenue DESC, CustomerID) AS t;
-- MySQL: ... ORDER BY Revenue DESC, CustomerID LIMIT 3
Answer 3. Change the CASE to WHEN PctBefore < 70 THEN 'A' WHEN PctBefore < 90 THEN 'B' ELSE 'C'. Riffa Mart (PctBefore 60.8) stays A, but Bahrain Tech (PctBefore 75.8) becomes B. In the B/C line, Isa Town Bakery (PctBefore 92.1) moves from B to C, and Manama Cafe (PctBefore 88.3) stays B.
Summary
- Pareto analysis sorts items from biggest to smallest and tracks the running share of the total.
- In SQL:
SUM() OVER (ORDER BY ... ROWS UNBOUNDED PRECEDING)for the running total,SUM() OVER ()for the grand total, and100.0 *for decimal percentages. - ABC classification puts items into classes by running percentage; base the cut-off on the percentage before each row.
NTILE()gives the opposite view: how much each fifth or tenth of customers brings in.- Measure the real split for your data; it’s rarely exactly 80/20.
Related articles
- Running Totals in SQL Server: the running SUM at the heart of this report.
- SQL Window Functions Explained Simply: ROW_NUMBER, NTILE and window frames.
- Basic Statistics in SQL: averages, medians and percentiles for the same kind of data.
- Cohort and Retention Analysis in SQL: another classic customer analysis.