Percentages of a Total
SUM() OVER ().
What share of all orders came in each month? To work out a percentage, every row needs to know the grand total. A window function with empty brackets gives exactly that.

- What share of all orders?
- Each row needs the total
- OVER with empty brackets
SUM() OVER ()
Sum of orders, over, with nothing inside the brackets, means, add up every row in the result. So each month is divided by the grand total. Run it. August twenty twenty five had seven and a half percent of all orders, over two years.
WITH monthly AS (
SELECT strftime('%Y-%m', order_date) AS month, COUNT(*) AS orders
FROM orders
GROUP BY month
)
SELECT month,
orders,
ROUND(100.0 * orders / SUM(orders) OVER (), 1) AS pct_of_all
FROM monthly
ORDER BY orders DESC
LIMIT 3;โโโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโโโโ โ month โ orders โ pct_of_all โ โโโโโโโโโโโผโโโโโโโโโผโโโโโโโโโโโโโค โ 2025-08 โ 120 โ 7.5 โ โ 2025-12 โ 104 โ 6.5 โ โ 2025-05 โ 103 โ 6.4 โ โโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโโโโ
The integer trap
Notice we wrote one hundred point zero, not one hundred. Here's why. Run it. One hundred and forty divided by one thousand five hundred and ninety eight gives zero, because whole numbers divide into whole numbers in many databases. Add a decimal point, and you get the real answer, about eight point eight percent.
SELECT 140 / 1598 AS integer_division,
100.0 * 140 / 1598 AS percentage;โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโ โ integer_division โ percentage โ โโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโค โ 0 โ 8.76095118898623 โ โโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโ
Cancellation rate
Your turn. What percentage of all orders were cancelled? Round to one decimal place. Pause and try. Here's a solution. Count the cancelled orders with case inside sum, times one hundred point zero, divided by count star. Eight point eight percent.
Percentage of orders that were cancelled
Show hint
100.0 * SUM(CASE ...) / COUNT(*)
Show solution
SELECT ROUND(100.0 * SUM(CASE WHEN status = 'cancelled'
THEN 1 ELSE 0 END) / COUNT(*), 1)
AS cancel_rate
FROM orders;โโโโโโโโโโโโโโโ โ cancel_rate โ โโโโโโโโโโโโโโโค โ 8.8 โ โโโโโโโโโโโโโโโ
Summary
Sum over empty brackets gives the grand total on every row. And always use one hundred point zero for percentages.
Key takeaways- SUM(x) OVER ()
- 100.0, not 100
- Round the result