# How to calculate the median in SQL (PostgreSQL and SQLite)

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


**Short answer:** In PostgreSQL use `percentile_cont(0.5) WITHIN GROUP (ORDER BY x)`. SQLite has no built-in median, so number the sorted rows and average the middle one or two: `AVG(x)` over the rows whose number is `(n + 1) / 2` or `(n + 2) / 2`. That second method works in both.

**The task:** Find the middle value of a column, the median, overall or per group.

### The median pet weight

PostgreSQL has an ordered-set aggregate for it; in SQLite the same answer comes from the row-number method.

PostgreSQL:

```sql
SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY weight_kg) AS median_kg
FROM pets;
```

SQLite:

```sql
SELECT AVG(weight_kg) AS median_kg
FROM (SELECT weight_kg,
        ROW_NUMBER() OVER (ORDER BY weight_kg) AS rn,
        COUNT(*) OVER () AS n
      FROM pets) t
WHERE rn IN ((n + 1) / 2, (n + 2) / 2);
```

Result (both engines):

| median_kg |
| --- |
| 4.9 |

### One query for both databases

With 20 pets, the middle rows are the 10th and 11th; their average is the median. With an odd count both expressions give the same row.

```sql
SELECT AVG(weight_kg) AS median_kg
FROM (SELECT weight_kg,
        ROW_NUMBER() OVER (ORDER BY weight_kg) AS rn,
        COUNT(*) OVER () AS n
      FROM pets) t
WHERE rn IN ((n + 1) / 2, (n + 2) / 2);
```

Result (both engines):

| median_kg |
| --- |
| 4.9 |

### The median per group

Partition both window functions by species.

```sql
SELECT species, AVG(weight_kg) AS median_kg
FROM (SELECT species, weight_kg,
        ROW_NUMBER() OVER (PARTITION BY species ORDER BY weight_kg) AS rn,
        COUNT(*) OVER (PARTITION BY species) AS n
      FROM pets) t
WHERE rn IN ((n + 1) / 2, (n + 2) / 2)
GROUP BY species
ORDER BY species;
```

Result (both engines):

| species | median_kg |
| --- | --- |
| bird | 0.1 |
| cat | 4.1 |
| dog | 15.5 |
| rabbit | 2.1 |

### percentile_cont doesn't exist in SQLite

```sql
SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY weight_kg) FROM pets;
```

PostgreSQL:

| percentile_cont |
| --- |
| 4.9 |

SQLite: error: near "(": syntax error

## How it works

- `ROW_NUMBER() OVER (ORDER BY weight_kg)`: Numbers the rows from lightest to heaviest.
- `COUNT(*) OVER ()`: The number of rows, on every row.
- `rn IN ((n + 1) / 2, (n + 2) / 2)`: Integer division picks the middle row for an odd count, or the two middle rows for an even count.
- `AVG(weight_kg)`: Averages those one or two rows: the median.

## Variations

### Other percentiles

In PostgreSQL, `percentile_cont(0.9)` gives the 90th percentile and `percentile_disc` returns a real value from the column instead of interpolating.

## Common mistakes

- Using AVG: the mean is pulled by extreme values; the median isn't.
- Forgetting NULLs: filter them out (`WHERE weight_kg IS NOT NULL`) before numbering, or they're counted.
- Picking only `n / 2` for an even count: that's the lower middle value, not the median.

## PostgreSQL and SQLite

PostgreSQL has `percentile_cont` and `percentile_disc`. SQLite has neither (the build shows the syntax error), so use the row-number method, which also runs on PostgreSQL. Some SQLite builds ship a `median()` extension, but standard SQLite doesn't.
