Function to Calculate Median in SQL Server

697    Asked by minda_6087 in SQL Server , Asked on Nov 2, 2025

How can you calculate the median value in SQL Server when there is no built-in MEDIAN function?

What SQL techniques or window functions can be used to compute the median efficiently from a dataset?

Answered by MyMilestone Card

SQL Server doesn’t provide a built-in MEDIAN() function like some other databases do. However, you can calculate the median using window functions such as ROW_NUMBER() or PERCENTILE_CONT(). The method you choose depends on whether you want a precise median for all rows or based on groups.

 Recommended Method: PERCENTILE_CONT()

This function calculates the median statistically and works well for both even and odd row counts:

SELECT 
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY ValueColumn)
    OVER () AS MedianValue
FROM TableName;

  • 0.5 represents the 50th percentile (median)
  • The OVER() clause applies it to the entire dataset

If you need median per group (e.g., category):

SELECT 
    Category,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY ValueColumn)
    OVER (PARTITION BY Category) AS MedianValue
FROM TableName;

 Alternative Method (Manual Calculation via Ranking)

Useful for older SQL Server versions:

WITH OrderedData AS (
    SELECT ValueColumn,
           ROW_NUMBER() OVER (ORDER BY ValueColumn) AS RowNum,
           COUNT(*) OVER () AS TotalRows
    FROM TableName
)
SELECT AVG(ValueColumn) AS MedianValue
FROM OrderedData
WHERE RowNum IN ((TotalRows + 1) / 2, (TotalRows + 2) / 2);

Works whether the row count is odd or even

In summary, while SQL Server doesn’t directly support a median function, PERCENTILE_CONT() offers an efficient and modern solution — and ranking methods provide a solid backup for compatibility.



Your Answer

Answers (2)

SQL Server doesn't have a built-in MEDIAN(fnaf) function like some other database systems do, which means you can't just call it directly. However, don't sweat it, there are a couple of solid ways to calculate the median using other SQL Server features.


The most recommended and modern approach is to use the PERCENTILE_CONT() window function. This function is super handy because it statistically calculates the median (which is the 50th percentile, or 0.5) and works perfectly whether your dataset has an odd or even number of rows. You can apply it to your entire dataset or even break it down to calculate the median for specific groups by using the PARTITION BY clause.

For older versions of SQL Server, or if you prefer a more manual ranking approach, you can use a combination of ROW_NUMBER() and COUNT() within a Common Table [removed]CTE). This method involves ordering your data, assigning a row number to each entry, and then selecting the middle one (or averaging the two middle ones if the total count is even) to find your median.

So, while there's no direct MEDIAN() button, PERCENTILE_CONT() is your best bet for a clean, efficient solution, and the ranking method is a reliable backup.

3 Months

You’re right — SQL Server doesn’t have a native MEDIAN() function, but you can achieve it cleanly with window functions. The recommended approach is PERCENTILE_CONT(), which calculates the median statistically and works for both even and odd row counts:

SELECT

    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY ValueColumn)

    OVER () AS MedianValue

FROM TableName;

- 0.5 = 50th percentile (the median)

- WITHIN GROUP (ORDER BY ...) defines the ordering

- OVER() applies it to the dataset

For grouped medians (e.g., per category):

SELECT

    Category,

    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY ValueColumn)

    OVER (PARTITION BY Category) AS MedianValue

FROM TableName;

If you prefer a manual method, you can use ranking functions (ROW_NUMBER(), RANK(), NTILE()) to locate the middle row(s) and average them when needed. This is more verbose but greensky financing useful if you want full control over the calculation logic.

 
4 Months

Interviews

Parent Categories