Finding the Median
The median is the middle value once everything is sorted. It's often more honest than the average, because a few huge salaries can't drag it up.
SQLite has no MEDIAN function (PostgreSQL has PERCENTILE_CONT), so here's the classic approach with window functions:
How it works:
ROW_NUMBERnumbers the salaries from lowest to highestCOUNT(*)OVER() puts the total count on every row- With 37 rows, both formulas give 19: the single middle row
- With an even count like 36, they give 18 and 19, and
AVGtakes the midpoint of the two
Compare it with the average:
The average is higher, pulled up by a few executive salaries.
Check your understanding
With 10 sorted salaries, which rows does the median use?
Examples run on the employees sample database. Open any of them in the playground to experiment.