Median explained simply

Train for your next tech interview
1,500+ real interview questions across engineering, product, design, and data — with worked solutions.
Join the waitlist

What median actually is

The median is the middle value of a sorted dataset. Half the observations sit above it, half sit below. That is the entire definition, but the consequences are surprisingly deep — once a distribution is skewed or has outliers, the median tells a very different story from the mean, and most product data is skewed.

Take five engineer salaries at Stripe: 150K, 170K, 190K, 210K, and 900K. Sorted, the middle value is 190K — that is the median. The mean works out to 324K, which describes nobody on the team. One staff engineer with a heavy equity refresh pulls the average above what every other person is paid. If a recruiter quoted you "average comp is 324K" and your offer came in at 200K, you would feel underpaid. If they quoted the median at 190K, you would feel that 200K is competitive. Same data, different number, different decision.

For an even-sized sample, take the two middle values and average them. Salaries 150K, 170K, 190K, 210K — the median is (170 + 190) / 2 = 180K. Some statistical software returns one of the two middles instead of the average (the "discrete" median); most analytics tools default to the continuous version where you interpolate between them. The differences are tiny on large samples.

Median vs mean

The mean is the sum divided by the count. The median is the order statistic at position N/2. Mean uses every observation and is pulled by extremes. Median uses only the rank of each observation and ignores the magnitude of the tails. That single property — robustness to outliers — is why product analysts reach for it.

The mean is computed in O(N) with one pass and can be aggregated across groups by summing the parts. The median needs a sort, costs O(N log N) in general, and is not additive — the median of a union is not a function of the medians of the parts. If you store daily medians in a metrics table, you cannot recover the weekly median from them. You need the raw rows.

On a normal distribution, mean and median are essentially equal. On a long-tailed distribution like revenue per user or session duration, they diverge — the mean drifts toward the tail, the median sits near the bulk of users. The gap between mean and median is itself a rough skewness indicator and a useful sanity check on any product dashboard.

When to use median

Skewed distributions are the obvious case. Order value, session length, customer lifetime value, page load time — all of these have fat right tails. The median answers "what does a typical user do?" The mean answers "what does the average user contribute to the total?" If your audience hears the word "typical" and pictures a normal person rather than a weighted-by-revenue robot, the median is the honest number.

Outlier-prone metrics are the second case. A single whale customer who spends a million dollars will shift the mean dramatically and barely move the median. If your CEO emails on Monday asking why average order value spiked, the answer is often that one customer placed an unusually large order. Reporting the median alongside the mean would have prevented the question.

Reporting to non-technical stakeholders is the third case. The mean of a long-tailed metric is genuinely confusing — most users sit well below it, which feels wrong to anyone reading the chart. The median sits near the center of the data as people perceive it. Engineering leadership at companies like Linear and Vercel routinely report P50 latency rather than mean latency for the same reason — it matches user intuition.

When to stick with the mean

If your metric is roughly symmetric — height, IQ, test scores, normalized engagement indices — mean and median converge and the mean is faster, additive, and supported by every standard statistical test. There is no reason to pay the sorting cost.

If you need totals, you need the mean. Total revenue equals mean revenue per user times user count. Median revenue per user has no such identity, so it cannot be used in budget forecasts or financial models. This is why finance teams report mean ARR per customer even when a product analyst would prefer the median.

If you plan to run a t-test, ANOVA, or any classical parametric test, those tests are about the mean. Comparing two medians needs a different machinery — Mann-Whitney U, bootstrap, or a permutation test. Mixing a median estimate with a t-test is a textbook interview mistake.

Median in SQL

The standard recipe across Postgres, Snowflake, and Oracle uses PERCENTILE_CONT. The function takes a percentile between zero and one and returns the interpolated value within a sorted group. Setting it to 0.5 returns the median:

-- Postgres, Snowflake, Oracle, Redshift
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) AS median_salary
FROM employees;

The same function exists in BigQuery but uses analytic-function syntax with an OVER clause, so you usually wrap it in a subquery and take the first row:

-- BigQuery
SELECT median_salary
FROM (
  SELECT PERCENTILE_CONT(salary, 0.5) OVER () AS median_salary
  FROM employees
)
LIMIT 1;

MySQL through version 8 has no built-in percentile function, so you fall back to row numbering. Rank every row by the target column, find the rank at position (N+1)/2, and average the one or two middle rows:

-- MySQL 8
SELECT AVG(salary) AS median_salary
FROM (
  SELECT
    salary,
    ROW_NUMBER() OVER (ORDER BY salary) AS rn,
    COUNT(*)    OVER ()                AS cnt
  FROM employees
) ranked
WHERE rn IN (FLOOR((cnt + 1) / 2), CEIL((cnt + 1) / 2));

For grouped medians — median order value by country, median LTV by acquisition channel — wrap the same expression in a GROUP BY and the engine handles the sort per group. Watch the runtime when groups are large; sorting hundreds of millions of rows per partition is where median queries start to hurt.

Train for your next tech interview
1,500+ real interview questions across engineering, product, design, and data — with worked solutions.
Join the waitlist

Median in Python

Both NumPy and pandas expose the median directly. NumPy is the fastest on raw arrays; pandas is convenient when you already have a DataFrame and want medians by group:

import numpy as np
import pandas as pd

salaries = [150_000, 170_000, 190_000, 210_000, 900_000]
np.median(salaries)             # 190000.0

df = pd.DataFrame({"salary": salaries, "level": ["L4", "L4", "L5", "L5", "L6"]})
df["salary"].median()           # 190000.0
df.groupby("level")["salary"].median()

# Percentiles for the full distribution shape
np.percentile(salaries, [25, 50, 75, 95, 99])

For very large arrays, NumPy uses a partitioning algorithm — O(N) on average rather than O(N log N) — so it stays fast even on tens of millions of values. If your data lives in a Polars or DuckDB frame, both expose .median() with similar semantics and excellent performance.

Median in A/B tests

Revenue, time-on-task, and order value are the metrics that most often need a median A/B test. The mean of these metrics is so dominated by tail users that a real change for the typical user gets buried. Median moves only when something meaningful happens in the bulk of the distribution.

import numpy as np

# A toy comparison
control = np.random.lognormal(mean=6.5, sigma=1.2, size=10_000)
treat   = np.random.lognormal(mean=6.55, sigma=1.2, size=10_000)

print(np.mean(control),   np.mean(treat))    # ~1280 vs ~1340  → mean says +4.7%
print(np.median(control), np.median(treat))  # ~ 665 vs ~ 695  → median says +4.5%

You cannot p-value the median difference with a t-test — there is no clean closed-form formula for the variance of a sample median under arbitrary distributions. The two standard tools are Mann-Whitney U, which tests whether one distribution is stochastically larger than the other, and bootstrap, which resamples the data thousands of times and reads off a percentile confidence interval. Bootstrap is the more flexible of the two and is the default at most modern experimentation teams; see bootstrap explained simply for the recipe.

A practical warning: powering an experiment on the median requires more samples than powering on the mean for the same relative effect size, because the median has higher variance per unit of data. Plan your sample size in advance — switching from a mean test to a median test mid-experiment after a peek is a peeking violation and biases the result.

Common pitfalls

The first pitfall is reporting median without context. Saying "median order value is seventy dollars" is fine, but it hides whether ninety percent of users sit between sixty-five and seventy-five, or whether half sit at thirty and half at four hundred. The fix is to always pair the median with at least one tail measure — P95 or the interquartile range — so the reader sees the spread, not just the center.

The second pitfall is trying to aggregate medians. Median is not additive. The median of a union of two groups is not the weighted average of the per-group medians. If you precompute daily medians and then want a weekly median, you need to go back to the raw rows. Storing pre-aggregated medians in a metrics warehouse is one of the most common analytics-platform footguns.

The third pitfall is testing median differences with a t-test. The t-test assumes the test statistic is approximately normal, which holds for sample means under the central limit theorem but does not hold cleanly for sample medians on skewed distributions. Use Mann-Whitney U or bootstrap when the metric of interest is the median itself.

The fourth pitfall is confusing median with mode. The median is the middle of the sorted data; the mode is the most frequent value. On continuous data the mode is rarely meaningful — every value appears once. On categorical or discrete data the mode can be the right answer when median is not defined. Interviewers love to test whether you can name all three central-tendency measures and pick the right one.

The fifth pitfall is forgetting that median is insensitive to extremes that the business cares about. If a fraud detection metric needs to flag the worst one percent of transactions, the median tells you nothing about that tail. Pair median with P95, P99, or maximum whenever the tail itself is the signal.

Interview answers

"What is the median?" The middle value of a sorted sample. For an even-sized sample, the average of the two middle values.

"When would you use median instead of mean?" When the distribution is skewed or has outliers, and you want to describe a typical observation rather than a total contribution. Revenue per user, session length, and load time are textbook cases.

"What does it mean if mean and median diverge?" The distribution is skewed. Mean greater than median means a long right tail; mean less than median means a long left tail. The gap is a rough skewness indicator.

"How do you compute the median in SQL?" PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY col) on every modern warehouse except MySQL, which needs a ROW_NUMBER workaround.

"How would you A/B test a median?" Bootstrap the difference of medians thousands of times and read off the 2.5th and 97.5th percentiles of the resampled differences. Or use a Mann-Whitney U test as a parametric-free alternative.

If you want to drill stats and SQL questions like this every day, NAILDD is launching with hundreds of interview problems across exactly this pattern.

FAQ

Should I report mean or median to my PM?

Report both when the distribution is skewed, which is most product metrics. The mean tells the finance team how revenue rolls up; the median tells the product team what a typical user does. Showing both also flags any divergence that hints at a tail problem worth investigating.

How do I compute the median in Excel?

Use =MEDIAN(range). The function handles even and odd sample sizes automatically and ignores empty cells. For grouped medians, build a pivot table with the value column set to median — modern Excel supports this directly.

Is the median sensitive to sample size?

The median is more stable than the mean against outliers, but it still has noise. For very small samples — under ten observations — both mean and median are unstable, and you should report a confidence interval rather than a point estimate. Bootstrap is the easiest way to get one without assuming a distribution.

Can I combine medians across groups?

Not by averaging the per-group medians. The median is not additive, so you have to recompute it from the underlying rows. The standard analytics-warehouse pattern is to keep the raw event table queryable rather than rely on pre-aggregated daily medians.

What is the difference between median, P50, and the second quartile?

They are three names for the same thing — the fiftieth percentile of the data. Some teams use "P50" in latency contexts, "median" in product reports, and "Q2" in textbook statistics. The number is identical.

Is median always better than mean for skewed data?

Almost always for describing a typical observation, never for computing totals, and rarely for parametric hypothesis tests. The right answer in most dashboards is to show both and let the reader compare them.