Median Calculator + SQL Query Builder
Enter values to compute the median instantly, then generate a SQL median query for your database engine.
What is median and why SQL users care
The median is the middle value in an ordered list. If the list has an odd number of rows, median is the center item. If the list has an even number of rows, median is the average of the two center items. In analytics, median is often preferred over average because it is less sensitive to outliers.
For example, in salary analysis, one executive with a very high salary can skew the average, but the median usually reflects the “typical” salary better.
Median vs average in practical reporting
- Average (mean): Sum of values divided by count. Easy to compute but can be distorted by extreme values.
- Median: Middle value after sorting. More robust for skewed data distributions.
- When to use median: Income, housing prices, order sizes, response times, and any metric with long tails.
Why median calculation in SQL is sometimes tricky
Unlike AVG() or SUM(), median is not universally available as a simple aggregate function across all database systems. Some engines provide native support, while others require window functions and ranking logic. The exact syntax differs by vendor.
Common issues developers face
- Different SQL dialects across PostgreSQL, MySQL, SQL Server, Oracle, and SQLite.
- Handling odd and even counts correctly.
- Excluding
NULLvalues from calculations. - Computing median per group (for example, median salary by department).
Median SQL patterns by database
PostgreSQL
PostgreSQL supports ordered-set aggregates such as PERCENTILE_CONT:
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) AS median
FROM employees
WHERE salary IS NOT NULL;
MySQL 8+
MySQL does not have a direct MEDIAN() aggregate, but window functions make it possible:
WITH ranked AS (
SELECT salary,
ROW_NUMBER() OVER (ORDER BY salary) AS rn,
COUNT(*) OVER () AS cnt
FROM employees
WHERE salary IS NOT NULL
)
SELECT AVG(salary) AS median
FROM ranked
WHERE rn IN (FLOOR((cnt + 1) / 2), FLOOR((cnt + 2) / 2));
SQL Server
SELECT DISTINCT
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) OVER () AS median
FROM employees
WHERE salary IS NOT NULL;
Oracle
Oracle includes a built-in MEDIAN aggregate:
SELECT MEDIAN(salary) AS median
FROM employees
WHERE salary IS NOT NULL;
SQLite
SQLite has no built-in median aggregate, so the common approach is ranking rows and averaging the middle one(s):
WITH ranked AS (
SELECT salary,
ROW_NUMBER() OVER (ORDER BY salary) AS rn,
COUNT(*) OVER () AS cnt
FROM employees
WHERE salary IS NOT NULL
)
SELECT AVG(salary) AS median
FROM ranked
WHERE rn IN (CAST((cnt + 1) / 2 AS INT), CAST((cnt + 2) / 2 AS INT));
Median by group (department, region, category)
Real-world reports often require medians per segment. Here is a PostgreSQL-style example:
SELECT department_id,
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) AS median_salary
FROM employees
WHERE salary IS NOT NULL
GROUP BY department_id;
In engines without native median, use partitioned window functions and aggregate the selected center rows per group.
Performance tips for large datasets
- Create an index on the numeric column used in
ORDER BY. - Filter early with
WHEREclauses to reduce ranked rows. - Pre-aggregate in materialized views for frequently queried medians.
- Benchmark with realistic row counts; median queries often require sorting.
Edge cases to handle correctly
- Even row count: Median is average of the two middle rows.
- Odd row count: Median is the middle row.
- NULL values: Usually exclude them with
WHERE col IS NOT NULL. - Data type precision: Use decimal types for financial values to avoid floating-point surprises.
Quick checklist for production SQL
- Confirm your database’s supported median syntax.
- Validate that your query handles odd and even counts.
- Test with known sample datasets.
- Review query plan for sort and window costs.
- Document assumptions (null handling, currency precision, grouping keys).
Median calculation in SQL is one of those topics that seems simple mathematically but requires careful query design in production systems. Use the calculator above to verify numbers quickly and generate engine-specific SQL templates you can adapt to your schema.