← Back to SQL, page 13

sql-0256 · Medium mid

How do you calculate the median salary in SQLite, which has no MEDIAN function, and in PostgreSQL?

In SQLite, number the sorted values and average the middle one or two: rows where row_number is in ((n + 1) / 2, (n + 2) / 2) with integer division. PostgreSQL has percentile_cont(0.5) WITHIN GROUP (ORDER BY salary), which interpolates between the two middle values for even counts.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

There are seven non-NULL salaries, so the median is the fourth:

sql
WITH s AS (
  SELECT salary, ROW_NUMBER() OVER (ORDER BY salary) AS rn, COUNT(*) OVER () AS n
  FROM employees WHERE salary IS NOT NULL
)
SELECT AVG(salary) AS median FROM s WHERE rn IN ((n + 1) / 2, (n + 2) / 2);
median
70000.0

For an even count, the two middle values are averaged. Removing Asha leaves six salaries:

sql
WITH s AS (
  SELECT salary, ROW_NUMBER() OVER (ORDER BY salary) AS rn, COUNT(*) OVER () AS n
  FROM employees WHERE salary IS NOT NULL AND name <> 'Asha'
)
SELECT AVG(salary) AS median FROM s WHERE rn IN ((n + 1) / 2, (n + 2) / 2);
median
66000.0

PostgreSQL:

sql
SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY salary) AS median_cont,
       percentile_disc(0.5) WITHIN GROUP (ORDER BY salary) AS median_disc
FROM employees;
median_contmedian_disc
70000.070000

percentile_disc(0.5) would return an actual value from the data (the lower middle one) instead of an average. NULL salaries are left out in both engines' versions, which matches how other aggregates behave.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

WITH s AS (
  SELECT salary, ROW_NUMBER() OVER (ORDER BY salary) AS rn, COUNT(*) OVER () AS n
  FROM employees WHERE salary IS NOT NULL
)
SELECT AVG(salary) AS median FROM s WHERE rn IN ((n + 1) / 2, (n + 2) / 2);

WITH s AS (
  SELECT salary, ROW_NUMBER() OVER (ORDER BY salary) AS rn, COUNT(*) OVER () AS n
  FROM employees WHERE salary IS NOT NULL AND name <> 'Asha'
)
SELECT AVG(salary) AS median FROM s WHERE rn IN ((n + 1) / 2, (n + 2) / 2);

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Median = middle value, or average of the two middle values
  • SQLite: ROW_NUMBER with the count, then average the middle rows
  • PostgreSQL: percentile_cont(0.5) WITHIN GROUP (ORDER BY x)
  • percentile_disc returns an existing value

Common mistakes

  • Using AVG and calling it a median
  • Off-by-one errors in the middle-row positions

Follow-ups

  • How would you compute the median per department?
  • When is the median more useful than the mean?

Tips

  • Check both odd and even counts