sqlstreak

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.

Each species' share of the pets

SELECT species,
       COUNT(*) AS pets,
       ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct
FROM pets
GROUP BY species
ORDER BY pct DESC, species;

Result (PostgreSQL and SQLite, identical): 4 rows

speciespetspct
dog840
cat735
rabbit315
bird210

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    COUNT(*)

    Each species' count, from GROUP BY.

  2. 2
    SUM(COUNT(*)) OVER ()

    The sum of all those counts: the grand total, repeated on every row. An empty OVER () means “all rows”.

  3. 3
    100.0 * … / …

    The decimal point makes it decimal division.

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

categorywhole_number_pctpct
pizza4545.5
side2727.3
drink1818.2
dessert99.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

categorynamerevenuepct_of_category
dessertTiramisu19.5100
drinkCola12.551
drinkLemon Soda1249
pizzaPepperoni Rocket94.529.5
pizzaFour Cheese Orbit7021.8
pizzaBBQ Meteor6219.3
pizzaMargherita5517.2
pizzaVeggie Nebula3912.2
sideWings3455.7
sideGarlic Knots2032.8
sideCaesar Salad711.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 / b with 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

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.

Start free