80/20 Pareto Analysis in SQL: Find Your Top Customers (With ABC Classes)

“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.

Ten customers sorted by revenue with a running total line crossing 80% after four customers, labelled class A, B and C, and the SUM() OVER window function used to compute it

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;
CustomerIDNameRevenueRunningRevenueRunningPctCustomerPct
1Al Noor Trading42000.0042000.0035.010.0
2Gulf Office Supplies31000.0073000.0060.820.0
3Riffa Mart18000.0091000.0075.830.0
4Bahrain Tech9000.00100000.0083.340.0
5Seef Stationery6000.00106000.0088.350.0
6Manama Cafe4500.00110500.0092.160.0
7Isa Town Bakery3500.00114000.0095.070.0
8Hidd Garage2500.00116500.0097.180.0
9Zallaq Resort2000.00118500.0098.890.0
10Budaiya Farm1500.00120000.00100.0100.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;
CustomerIDNameRevenueRunningPctClass
1Al Noor Trading42000.0035.0A
2Gulf Office Supplies31000.0060.8A
3Riffa Mart18000.0075.8A
4Bahrain Tech9000.0083.3A
5Seef Stationery6000.0088.3B
6Manama Cafe4500.0092.1B
7Isa Town Bakery3500.0095.0B
8Hidd Garage2500.0097.1C
9Zallaq Resort2000.0098.8C
10Budaiya Farm1500.00100.0C

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;
ClassCustomersRevenuePctRevenue
A4100000.0083.3
B314000.0011.7
C36000.005.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;
QuintileCustomersRevenuePctRevenue
1273000.0060.8
2227000.0022.5
3210500.008.8
426000.005.0
523500.002.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 default RANGE frame they’d even share one running total. Add a unique column and ROWS 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, and 100.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

Scroll to Top