SQL how-to
How to calculate a percentage of the total in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
Divide each group's value by the grand total from a window function: 100.0 * COUNT(*) / SUM(COUNT(*)) OVER (). Write 100.0, not 100, so the division isn't done in whole numbers, and round at the end.
The task: Show each group's share of the total, as a percentage.
How it works
- 1
COUNT(*)Each species' count, from GROUP BY.
- 2
SUM(COUNT(*)) OVER ()The sum of all those counts: the grand total, repeated on every row. An empty OVER () means “all rows”.
- 3
100.0 * … / …The decimal point makes it decimal division.
- 4
ROUND(…, 1)One decimal place, at the very end.
More examples
The integer division trap
Whole numbers divided by whole numbers stay whole in both databases. 5 of the 11 menu items are pizzas, which is 45.5%, but COUNT(*) * 100 / 11 says 45; the dessert's 9.1% becomes 9.
SELECT category,
COUNT(*) * 100 / (SELECT COUNT(*) FROM menu_items) AS whole_number_pct,
ROUND(100.0 * COUNT(*) / (SELECT COUNT(*) FROM menu_items), 1) AS pct
FROM menu_items
GROUP BY category
ORDER BY pct DESC;Result (PostgreSQL and SQLite, identical): 4 rows
| category | whole_number_pct | pct |
|---|---|---|
| pizza | 45 | 45.5 |
| side | 27 | 27.3 |
| drink | 18 | 18.2 |
| dessert | 9 | 9.1 |
Try it: PostgreSQL playground · SQLite playground
Share within each group
Each item's share of its own category's revenue: the window total is partitioned by category.
SELECT m.category, m.name,
SUM(oi.quantity * m.price) AS revenue,
ROUND(100.0 * SUM(oi.quantity * m.price)
/ SUM(SUM(oi.quantity * m.price)) OVER (PARTITION BY m.category), 1) AS pct_of_category
FROM order_items oi
JOIN menu_items m ON m.id = oi.item_id
GROUP BY m.category, m.name
ORDER BY m.category, revenue DESC;Result (PostgreSQL and SQLite, identical): 11 rows
| category | name | revenue | pct_of_category |
|---|---|---|---|
| dessert | Tiramisu | 19.5 | 100 |
| drink | Cola | 12.5 | 51 |
| drink | Lemon Soda | 12 | 49 |
| pizza | Pepperoni Rocket | 94.5 | 29.5 |
| pizza | Four Cheese Orbit | 70 | 21.8 |
| pizza | BBQ Meteor | 62 | 19.3 |
| pizza | Margherita | 55 | 17.2 |
| pizza | Veggie Nebula | 39 | 12.2 |
| side | Wings | 34 | 55.7 |
| side | Garlic Knots | 20 | 32.8 |
| side | Caesar Salad | 7 | 11.5 |
Try it: PostgreSQL playground · SQLite playground
Variations
Share of a condition
The percentage of adopted pets: ROUND(100.0 * SUM(CASE WHEN adopted THEN 1 ELSE 0 END) / COUNT(*), 1).
Percentages that don't add up to 100
Rounding each share can leave the total at 99.9 or 100.1. Round once for display, or show one more decimal.
Common mistakes
100 * a / bwith whole numbers: integer division throws away the fraction.Rounding before dividing: round the final percentage, not the parts.
Dividing by a total that can be 0: wrap it in
NULLIF(total, 0).
PostgreSQL and SQLite
Everything on this page works the same in PostgreSQL and SQLite: each example above was run on both, and the results match.
Official documentation
Go deeper
- Guide: SQL window frames, moving averages, NTILE and ROLLUP
- Lesson: Frames and shares (Pro)
Make it stick in five minutes a day
“Frames and shares” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.