# How to calculate a percentage of the total in SQL

Source: https://sqlstreak.com/sql/how-to/percentage-of-total · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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

```sql
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 (both engines):

| species | pets | pct |
| --- | --- | --- |
| dog | 8 | 40 |
| cat | 7 | 35 |
| rabbit | 3 | 15 |
| bird | 2 | 10 |

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

```sql
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 (both engines):

| category | whole_number_pct | pct |
| --- | --- | --- |
| pizza | 45 | 45.5 |
| side | 27 | 27.3 |
| drink | 18 | 18.2 |
| dessert | 9 | 9.1 |

### Share within each group

Each item's share of its own category's revenue: the window total is partitioned by category.

```sql
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 (both engines):

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

(8 of 11 rows)

## How it works

- `COUNT(*)`: Each species' count, from GROUP BY.
- `SUM(COUNT(*)) OVER ()`: The sum of all those counts: the grand total, repeated on every row. An empty OVER () means “all rows”.
- `100.0 * … / …`: The decimal point makes it decimal division.
- `ROUND(…, 1)`: One decimal place, at the very end.

## 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

Same in both engines.
