Question: How do you find the median in SQL Server?
Answer: First sort the values. With an odd count, the median is the middle value. With an even count, it is the average of the two middle values. SQL Server’s PERCENTILE_CONT(0.5) gives that interpolated middle value for a numeric set. The original example below uses sales order details, so you can see the answer for more than one group.


Find the median for each sales order
The original article used a sample database named AdventureWorks. This runnable version uses AdventureWorks2025 with the same SalesOrderDetail objects and order IDs. PERCENTILE_CONT is available from SQL Server 2012; its result is float(53) and NULL input values are ignored. PARTITION BY SalesOrderID gives each order its own median, and ORDER BY ProductID tells the function which values to place in order. The median appears on every detail row in that order because this is a window function.
USE AdventureWorks2025;
GO
SELECT SalesOrderID,
OrderQty,
ProductID,
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY ProductID)
OVER (PARTITION BY SalesOrderID) AS MedianCont
FROM Sales.SalesOrderDetail
WHERE SalesOrderID IN (43670, 43669, 43667, 43663)
ORDER BY SalesOrderID DESC, ProductID, SalesOrderDetailID;
GOHere is the original result from that sample data. In order 43670, the two middle ProductID values are 710 and 773, so the median is 741.5. In order 43667, the middle values are 773 and 775, so the median is 774. The highlighted result is the useful part of this example, so it belongs beside the SQL.

Those values depend on the rows in the sample database version you run. If your AdventureWorks data differs, check the ordered ProductID values rather than expecting the same screenshot. Also notice the function name: PERCENTILE_CONT, not PERCENTILE_COUNT. For an even group it can interpolate between observed values, so 741.5 need not itself be a ProductID in the table.
That is the interview answer: define the middle first, partition by the group whose median you want, and use the result to explain why an even-sized set may produce a value between two rows. For more detail, see my earlier posts on T-SQL medians and PERCENTILE_CONT.
Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.
Discover more from SQL Authority with Pinal Dave
Subscribe to get the latest posts sent to your email.





6 Comments. Leave new
Thank you very much for the content. I learned something new.
That’s really great Nagaraj!
Great article! Thanks a lot. I do have a question though: how do you implement the logic of even and odd numbers in the SQL Query? Or is that already taken care of by using the PERCENTILE_COUNT?
Exactly what I wanted. My boss said GET ME THE MEDIAN PRODUCT ID BY LUNCH TIME! Now I have it.
If someone asked me that question on an interview, I would think considerably less about my interviewers. Unless you’re working with statistics every day, this is definitely in the category of “Let me Google that for you”… look it up when you need it, otherwise just remember that there is no MEDIAN aggregate function in TSQL (for some bizarre reason)…
even/odd is taken care of by using PERCENTILE_CONT. Another option is to use PERCENTILE_DISC which performs differently.