How to Calculate a Median in BigQuery

Last updated August 30, 2026 · By the SaturnSQL team

BigQuery has no MEDIAN() function. Use PERCENTILE_CONT(col, 0.5) OVER () for an exact median, or APPROX_QUANTILES(col, 100)[OFFSET(50)] when you want a fast approximate median inside a normal GROUP BY.

SELECT DISTINCT PERCENTILE_CONT(order_total, 0.5) OVER () AS median_order
FROM orders;

Why the DISTINCT and OVER ()

PERCENTILE_CONT is a window function, not an aggregate, so it cannot appear next to GROUP BY. It returns the same interpolated median on every row, and DISTINCT collapses that to one. NULLs are ignored, and with an even row count you get the midpoint of the two middle values.

Median per group

For grouped medians the window form needs PARTITION BY plus DISTINCT. The aggregate APPROX_QUANTILES fits GROUP BY directly and is much cheaper on large tables; it splits values into 100 quantiles, so element 50 is the (approximate) median.

SELECT
  customer_id,
  APPROX_QUANTILES(order_total, 100)[OFFSET(50)] AS approx_median
FROM orders
GROUP BY customer_id;

Exact median per group

SELECT DISTINCT
  customer_id,
  PERCENTILE_CONT(order_total, 0.5) OVER (PARTITION BY customer_id) AS median_order
FROM orders;

Run this in SaturnSQL

SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.

Try it free

Do more with BigQuery

Related BigQuery guides