SQL how-to
How to calculate the median in SQL (PostgreSQL and SQLite)
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
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
SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY weight_kg) AS median_kg
FROM pets;SQLite
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 (PostgreSQL and SQLite, identical): 1 row
| median_kg |
|---|
| 4.9 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
ROW_NUMBER() OVER (ORDER BY weight_kg)Numbers the rows from lightest to heaviest.
- 2
COUNT(*) OVER ()The number of rows, on every row.
- 3
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.
- 4
AVG(weight_kg)Averages those one or two rows: the median.
More examples
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.
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 (PostgreSQL and SQLite, identical): 1 row
| median_kg |
|---|
| 4.9 |
Try it: PostgreSQL playground · SQLite playground
The median per group
Partition both window functions by species.
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 (PostgreSQL and SQLite, identical): 4 rows
| species | median_kg |
|---|---|
| bird | 0.1 |
| cat | 4.1 |
| dog | 15.5 |
| rabbit | 2.1 |
Try it: PostgreSQL playground · SQLite playground
percentile_cont doesn't exist in SQLite
SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY weight_kg) FROM pets;PostgreSQL: 1 row
| percentile_cont |
|---|
| 4.9 |
SQLitenear "(": syntax error
Try it: PostgreSQL playground · SQLite playground
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 / 2for 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.
Official documentation
Go deeper
- Guide: SQL interview questions: the core patterns
- 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.