Recipe · SQL recipes

Percentage of the total in SQL with SUM() OVER()

Divide each amount by SUM(amount) OVER () and multiply by 100.0. Use NULLIF on the denominator if the total can be zero.

Query

In the teaching database, orders have three statuses: shipped, cancelled and pending. First we add up the amount per status, then we work out what percentage of total sales each subtotal represents.

SQLite · run on the teaching database
WITH by_status AS (
  SELECT status,
         ROUND(SUM(total), 2) AS amount
  FROM orders
  GROUP BY status
)
SELECT status,
       amount,
       ROUND(
         100.0 * amount / NULLIF(SUM(amount) OVER (), 0),
         2
       ) AS total_percentage
FROM by_status
ORDER BY total_percentage DESC;

The percentages shown add up to 99.99 because each one is rounded to two decimal places. Before rounding, every row is calculated against the same total: 3931.90.

How it works

The CTE first creates one row per group. Then SUM(amount) OVER () adds up those amounts without regrouping them. 100.0 forces decimal arithmetic on engines or types where dividing integers could drop the fractional part. NULLIF(..., 0) avoids using zero as the denominator and returns NULL when the percentage is not mathematically defined.

Cases that change the result

How to read the query, step by step

  1. Summarise the unit you want to compare first. The CTE by_status produces one row per status.
  2. Compute the denominator without grouping again. SUM(amount) OVER () sees the three CTE rows and gets 3931.90 for each of them.
  3. Divide the part by the total. Each amount is divided by that shared denominator and multiplied by 100.
  4. Round at the end. That stops early rounding from needlessly changing the calculation.

Mental model: GROUP BY builds the parts; SUM(...) OVER () looks at the whole set of parts again without making them disappear.

Why OVER () changes the result

Without OVER, SUM(amount) would be an ordinary aggregate, and you would need another grouping or a subquery to combine it with each row. With OVER (), the sum becomes a window aggregate: it computes over the visible set but keeps one output per row.

If you add PARTITION BY, the denominator is no longer global. For example, SUM(amount) OVER (PARTITION BY month) would let you calculate each category's percentage within its month. The business question must decide which denominator is right.

Avoid this confusion

Using the number of rows as the denominator

COUNT(*) answers “how many rows are there”. If you want a percentage of sales, the denominator must be the sum of the amounts, not the number of groups.

Dividing integers and losing the decimals

-- May truncate depending on the engine and data types
amount / total * 100

Multiplying by 100.0 before dividing, or converting explicitly to a decimal type, makes it clear you expect a fractional result.

Confusing an overall percentage with a percentage within a larger group

OVER () uses all visible rows as a single partition. If you need “percentage within each month”, “within each country” or “within each order”, you must define that context with PARTITION BY.

Expecting rounded percentages to add up to exactly 100

Rounding each row separately can give 99.99 or 100.01. That is not necessarily a calculation error: it is a side effect of rounding each part.