# Calculate Median in SQL - Scaler Topics (2024)

## Overview

Median is a term widely used in statistical theory to calculate the middle value after sorting a list of numbers. It is used to separate small valued numbers from large numbers in a dataset. As the median splits the dataset into two halves, it is collectively used in finding the central tendency and to derive better results from a large dataset. In SQL also, the median is particularly used to find the middle range of column values in a table. For example, we can use the median to measure the central average value for income distributions, marks of students, and stock fluctuations.

SQL has no direct function for computing the intermediate value of a list of numbers. Therefore, in this article, we will discuss various methods used to find the median value for a given dataset.

## Scope

• In this article, we can take a brief overview of the median in SQL
• We will see different methods to find the median in SQL
• We will see why calculating the median in SQL is taken into consideration as hard
• We will find the median of a list of numbers using a simple SQL query
• We will see how to calculate the median using transact SQL, ranking function, and common table expressions
• In the end, we will create a function in SQL to calculate the median value

Median is a highly used term in the case of large data sets to find the middlemost number in a sorted list of values. If the dataset contains an odd number of values, then the median will be the middle value of the list of numbers and if the dataset contains an even number of values, then, the median will be the average of the two middle values of the list of numbers.

If the number of values is odd, the median can be calculated as:

We can understand this with the help of an example, suppose, we have 5 numbers in a list, {6, 89, 19, 3, 5}. After sorting, the list will be changed to {3, 5, 6, 19, 89}. Now, the middle element of the list is the median, i.e 6. We can also calculate it using the above formula (5 + 1) / 2 = 3rd item of the sorted list, that is 6.

If the number of values is even, the median can be calculated as:

Where n is the number of values in the dataset.

To understand this, let's say we have 10 numbers in a list, {7, 1, 3, 5, 9, 0, 2, 99, 12, 100}. After sorting this list, we will get {0, 1, 2, 3, 5, 7, 9, 12, 99, 100}.

Now, we can split this list into two halves {0, 1, 2, 3} and {9, 12, 99, 100}. The middle elements left are 5 and 7, the average of them will be the median of our list of numbers. So, the median is (5 + 7) / 2 = 6. We can also do it using the above formula for, ((10/2)th element + (10/2 + 1)th element)/2 = (5th element + 6th element)/2 = (5 + 7)/2 = 6.

As we know, in SQL, we have pre-defined aggregate functions used to obtain multiple results from a given set of values, like, the SUM method to find the sum of all the values in a list, the MIN function to find the minimum value in a set of numbers, MAX function to find the maximum value in a set of numbers, and the AVG function to calculate the average of numbers in a list.

But, unfortunately, there is no predefined function to calculate the median of numbers. So, developers need to write queries to find the median. In this article, we will look at different approaches to do the same.

## Why is Calculating Median Hard?

It is considered hard to calculate the median in SQL, as there is no direct pre-defined function to calculate the median value of a given set of numbers in a column, and so developers have to write queries using several in-built techniques in SQL like MAX, MIN, SUM, AVG.

For the standard aggregate functions (like min, max, count, etc.) it’s possible to get the aggregated result in a single traversal over a set of data. Median cannot work like that because it’s algorithmically much more complicated as so there is no direct method of finding it. The median is the middlemost value in a sorted list of items. So, the data needs to be sorted so that the middle value can be found.

To calculate the median of values, let’s first create a sample table. Suppose you have a student_details table, containing roll_number, and marks as the fields and some data has been inserted in these fields.

We have now created a table student_details, let’s insert some data into our fields:

We have now inserted some data in our fields of the student_details table. Now, let’s see what our table looks like, by using the SELECT query:

The output of the above query would be:

Now, let's calculate the median for the values in the marks column:

Let's now understand how this query is getting executed:

1. Firstly, we have initialized a variable named index in SQL having a single @ as the first character with the statement SET @index := -1.

2. Then, we can begin with the internal subquery.

In which we can sort the student_detail table in ascending order for the marks field, and then increment the index for each roll number and select the index with the corresponding marks.

The result of the above query would be:

1. We have kept the result of the internal subquery with the alias as m.

2. Now, we can check if the index we received as a result of the internal subquery is the middle index or not using the WHERE clause with the statement (WHERE m.i IN (FLOOR(@index / 2), CEIL(@index / 2));). For the case, when the number of elements in the list is even, we have taken the floor and the ceiling value of the number.

3. The FLOOR and the CEIL are the pre-defined functions in SQL, where FLOOR is used to return the largest integer value that is smaller than or equal to a number, and CEIL is used to return the smallest integer value that is bigger than or equal to a number.

4. In this way, the outer query will fetch the middle items of the given list of numbers.

5. Then, the SELECT clause of the outer query (SELECT AVG(m.marks) as Median) will return the average of those two middle values (selected in step 4) in the case of an even number of elements, and in the case of an odd number of elements, those two middle numbers will be the same.

6. The average is calculated using the built-in function in SQL, i.e AVG.

The output of the above complete query would be:

## Calculate Median Value Using Transact SQL

Transact SQL (T SQL) is the extended and advanced version of SQL. It is the product of Microsoft, which is used for performing the operations of procedural programming, string analyzing, manipulation, mathematics, and declaring local variables.

One of the most important features of T SQL is the stored procedure, which can be compiled and stored and then can later be executed when called. User-defined functions in SQL are a part of T SQL.

As we know that it is difficult to find the value of the median in a given list of items by using Transact Structured Query Language as there is no predefined built-in function available, and so we need to design our functions to calculate the value.

In the upcoming sections, will see different query methods in T SQL to calculate the value of the median in a dataset.

This method is the easiest method to find the Median. We can start by separating the dataset into two halves, the lower half and the upper half. To apply this, we can run the following SQL query on the above example of the student_details table, having roll_number and marks as the fields:

As we can see this query works by separating and grouping the list into 50 percent highest and 50 percent lowest values.

The output of the above query would be:

## Calculate Median Value Using PERCENTILE_CONT

The PERCENTILE_CONT function is used to find the value at a specific percentile in a given list of values. If there is no value at the exact location obtained, then PERCENTILE_CONT interpolates the answer.

The PERCENTILE_CONT function assumes a continuous distribution between the values of the expression and the sort specification. It tries to find the value at the exact location, but if no value was obtained, then it interpolates the value of that expression at the given percentile, performing linear interpolation.

The syntax for calculating PERCENTILE_CONT is:

Here, only a single column is allowed in the ORDER BY clause, and this column is the one for which the percentile is to be calculated. By default ORDER BY clause sorts the data in ascending order. The OVER() clause is used to define how to partition the input set. In our case, we want to have a single partition (as a whole list of values) and so, no PARTITION BY clause is specified. The percentile value which is taken as a parameter is a numeric value between 0 and 1.

To compute the median value using PERCENTILE_COUNT, we have to use the percentile value as 0.5.

To see the use of the PERCENTILE_COUNT function to calculate the median, let's see an example:

Suppose you have a stocks table, containing stock_id, and price as the fields, and some data has been inserted in these fields.

We have now created a table stocks, let’s insert some data into our fields:

We have now inserted some data in our fields of the stocks table. Now, let’s see what our table looks like, by using the SELECT query:

The output of the above query would be:

Now, let's calculate the median for the values in the price column:

The output of the above query is:

We can create a function in transact SQL to find the value of the median in a given list of numbers. It takes a table name and the column name (column for which the median is to be calculated) as a parameter. Then, it creates a temporary table with another name and stores the column values in a sorted manner. Then, it uses a dynamic scrollbar to move to the middle of the list of items and gives the middle value as a median.

We can create the function as:

Now, we can compile and save this procedure. Then, we’ll execute this procedure to get the median value of the Marks column from the student details Table. To run the procedure, execute the following query:

The median for the above student details table example is:

## Important Resources

1. SQL Queries Interview Questions and Answers

## Conclusion

• Median is the middlemost value in a sorted list of numbers.
• IN SQL, we don't have a direct function to calculate the medium of a list of numbers.
• If a dataset contains an odd number of values, then the median will be the middle value of the list of numbers and if the dataset contains an even number of values, then, the median will be the average of the two middle values in the list of numbers.
• Calculating the median is considered hard, as there is no direct pre-defined function to calculate the median value of a given set of numbers in a column in SQL, and so developers have to write queries using several in-built techniques in SQL like MAX, MIN, SUM, and AVG.
• Median can be calculated by writing a Simple SQL Query, along with the use of built-in functions in SQL.
• Median can be calculated using Transact SQL, like by the PERCENTILE_CONT method, Ranking Function, and Common Table Expressions.
• PERCENTILE_CONT is an inverse distribution function. It takes numbers as a continuous distribution between sets of values. Then, it interpolates the value of that expression at the given percentile, if the exact value can't be returned.
• For finding the median, the percentile value in the PERCENTILE_CONT function is taken as 0.5.
• In Transact Structured Query Language, stored procedures can be created, which can be compiled and stored and then can later be executed when called.
• Ranking method is the simplest method that works by separating and grouping the list into 50 percent highest and 50 percent lowest values.

## FAQs

### What is the easiest way to calculate median in SQL? ›

Median can be calculated by writing a Simple SQL Query, along with the use of built-in functions in SQL. Median can be calculated using Transact SQL, like by the PERCENTILE_CONT method, Ranking Function, and Common Table Expressions. PERCENTILE_CONT is an inverse distribution function.

How do you find the median of a large scale dataset? ›

Count how many numbers are in the set. Find the pair of numbers in the middle of the figures. Find the pair's average by adding them together and dividing by two. The resulting number is the median.

How do you find the median of grouped data in SQL? ›

To get the median we have to use PERCENTILE_CONT(0.5). If you want to define a specific set of rows grouped to get the median, then use the OVER (PARTITION BY) clause. Here I've used PARTITION BY on the column OrderID so as to find the median of unit prices for the order ids.

How to calculate median in spark SQL? ›

2. Calculating Median in Spark
1. // Calculate the median val medianValue = df. select(median(col("Price"))). first(). ...
2. // Calculate quantiles val quantileProbabilities = Array(0.5) val quantiles = df. stat. ...
3. // Calculate quantiles val quantileProbabilities = Array(0.25, 0.5, 0.75) val quantiles = df. stat.
May 14, 2023

What is the quickest way to find the median? ›

For a small data set, you first count the number of data points (n) and arrange the data points in increasing order. If the number of data points is uneven, you add 1 to the number of points and divide the results by 2 to get the rank of the data point whose value is the median.

How do you find the median of a large sample size? ›

Order the values of a data set of size n from smallest to largest. If n is odd, the sample median is the value in position (n + 1)/2; if n is even, it is the average of the values in positions n/2 and n/2 + 1.

How do you find the median of a scale? ›

The median is calculated by arranging the scores in numerical order, dividing the total number of scores by two, then rounding that number up if using an odd number of scores to get the position of the median or, if using an even number of scores, by averaging the number in that position and the next position.

How are the data sorted to calculate the median? ›

The median is the middle value in a set of data. First, organize and order the data from smallest to largest. To find the midpoint value, divide the number of observations by two. If there are an odd number of observations, round that number up, and the value in that position is the median.

What is the median value in SQL table? ›

What is a median? In simple words, the median is the middle value of a range of values in sorted order. Let's say that if we have a range of values from 1 to 11, the number 6 will be the median as it's the middle value that sits between the upper half and lower half of the range.

How to find the median from a frequency table with grouped data? ›

To find the median, add up the frequency column to find how many trains there were in total. There were 44 trains in total in this grouped frequency table, so work out 44 + 1 2 = 45 2 = 22.5. The median is therefore between the 22nd and 23rd values. Work down the frequency column, adding up frequencies as you go.

### What is the difference between average and median in SQL? ›

The average is calculated by adding up all the values and dividing the sum by the total number of values. The median can be calculated by listing all numbers in ascending order and then locating the number in the centre of that distribution.

Is median an aggregate function in SQL? ›

The MEDIAN function returns the median value in a set of values. The schema is SYSIBM. An expression that specifies the set of values from which the median is determined.

How do you find the median without a formula? ›

If the number of terms is even, the median is the mean of the two middle numbers. Arrange the numbers in order by size. If the number of terms is odd, the median is the middle term. If the number of terms is even, add the two middlemost terms and then divide by 2.

How do I find the mean and median? ›

To find the mean, add up the values in the data set and then divide by the number of values that you added. To find the median, list the values of the data set in numerical order and identify which value appears in the middle of the list.

How do you find the median solved examples? ›

For example, the median of 3, 3, 5, 9, 11 is 5. If there is an even number of observations, then there is no single middle value; the median is then usually defined to be the mean of the two middle values: so the median of 3, 5, 7, 9 is (5+7)/2 = 6.

What is the first step in calculating the median of a discrete variable? ›

To find the median, first order your data. Then calculate the middle position based on n, the number of values in your data set.

Does sample size affect median? ›

The value of sample median does not affect by the sample size, since it is the middle value of data set.

What is the formula of median question? ›

The steps to find the median are as follows: Step 1: Arrange the given data in ascending order. Step 2: Count the number of observations (n) to check whether it is odd or even. Step 3: If the number of observations (n) is odd, use the formula [(n +1)/2]th term to find the median.

What is the median of 1 2 3 4 5 6 7? ›

1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Where the number of terms is in even. Therefore, the median of the first 10 natural numbers is 5.5.

How do you find the median without outliers? ›

Identify the position of the median.

If your data set is large, you can identify the middle position by dividing the total number of data points by two and rounding up to the nearest whole number. Rounding up we get 5. The median will be the 5th value in the data set.

### Can you calculate median without sorting? ›

You can certainly find the median of an array without sorting it. What is not easy is doing that efficiently. For example, you could just iterate over the elements of the array; for each element, count the number of elements less than and equal to it, until you find a value with the correct count.

What is the first step in calculating the median of grouped data? ›

Step 1: Arrange the observations in ascending or descending order of magnitude. Step 2: Determine the total number of observations, say, Step 3: If is odd then the median = value of ( n + 1 2 ) th observation. If is even then the median = arithmetic mean of the value of ( n 2 ) th and ( n 2 + 1 ) th observation.

Do you have to rearrange to find median? ›

The median of a given data set or observations is the middle-most value after arranging the data in an ascending or a descending order. Q. Q. To find the median, the data should be arranged in ascending or descending order.

How to find median in Oracle SQL? ›

To calculate the median in Oracle SQL, we use the MEDIAN function. The MEDIAN function returns the median of the set of provided values. The MEDIAN is the middle value in a set of values.

How do I average a column in SQL? ›

If you'd like to calculate the average of numeric values stored in a column, you can do so using the AVG() aggregate function; it takes as its argument the name of the column whose average you want to calculate.

How do you find the middle value in SQL? ›

MySQL MID() Function

The MID() function extracts a substring from a string (starting at any position). Note: The MID() and SUBSTR() functions equals the SUBSTRING() function.

What is the formula for median with frequency? ›

If the data set is large we can us the formula to work out the position of the median Position of the median = ( n + 1 2 ) th \text{Position of the median}=(\frac{n+1}{2})^\text{th} Position of the median=(2n+1)th, and cumulative frequency to find the actual median value.

What is the formula of median for discrete grouped data? ›

Formula to Find Median for Discrete Series

The data is arranged in ascending or descending order. If it is an odd-sized sample, median = value of ([n + 1] / 2)th item. If it is an even-sized sample, median = ½ [ value of (n / 2)th item + value of ([n / 2] + 1)th item]

How do you find the mean median and mode in a frequency table? ›

How To Obtain The Mean, Median And Mode From A Frequency Table? To find the mean: Multiply midpoints by frequencies, add the subtotals and divide by the total of the frequencies. To find the mode: Look for the largest frequency and the corresponding value is the modal value or modal class.

Why do we use median instead of average? ›

It's best to use the mean when the distribution of the data values is symmetrical and there are no clear outliers. It's best to use the median when the the distribution of data values is skewed or when there are clear outliers.

### Which is better for data mean or median? ›

The mean is typically better when the data follow a symmetric distribution. When the data are skewed, the median is more useful because the mean will be distorted by outliers.”

Why is median better than average? ›

The advantage of the median is that it removes extreme measurements from a data set, giving you a more realistic idea of what to expect. The average can give you a more accurate sense of the total scope of data, but when it comes to planning for expenses, the median is a more reliable guide of expectations.

How do you find the median of a data table? ›

Remember, when you are working out the median:
1. Put the results in numerical order (in a frequency table this will already be done)
2. Count the total amount of results and add one.
3. Divide this by 2 to find the the position of the middle result.
4. Find the middle result in the numerically ordered list or frequency table.

What is the median value of a column? ›

Median is the value in the column which divides the dataset into two equal halves (i.e. the middle value).

What is the formula for the median of group data? ›

For example, 6, 4, 7, 3 and 2 is the given data set. To find the median of the given dataset, arrange it in ascending order. Therefore, the dataset is 2, 3, 4, 6 and 7. Hence, median = (n+1)/2 th observation.

What is an example of median calculation? ›

For example, the median of 3, 3, 5, 9, 11 is 5. If there is an even number of observations, then there is no single middle value; the median is then usually defined to be the mean of the two middle values: so the median of 3, 5, 7, 9 is (5+7)/2 = 6.

What is the formula for sample median? ›

In general, for a data set of n values, the sample median is the [ ( n + 1 ) / 2 ] -smallest value when n is odd and is the average of the ( n / 2 ) -smallest value and the ( n / 2 + 1 ) -smallest value when n is even.

How to calculate data in SQL query? ›

You can use the string expression argument in an SQL aggregate function to perform a calculation on values in a field. For example, you could calculate a percentage (such as a surcharge or sales tax) by multiplying a field value by a fraction.

How to check query statistics in SQL Server? ›

To view live query statistics for one query

To view the live query execution plan, on the tools menu click the Include Live Query Statistics icon. You can also view access the live query execution plan by right-clicking on a selected query in Management Studio and then click Include Live Query Statistics.

How to find data in SQL query? ›

The SQL SELECT Statement
1. SELECT column1, column2, ... FROM table_name;
2. SELECT * FROM table_name;
3. ExampleGet your own SQL Server. SELECT CustomerName, City FROM Customers; Try it Yourself »
4. Example. SELECT * FROM Customers; Try it Yourself »

### What is the median of this set of data values? ›

The median is the middle value in a set of data. First, organize and order the data from smallest to largest. To find the midpoint value, divide the number of observations by two. If there are an odd number of observations, round that number up, and the value in that position is the median.

How do you find the mean and median of a set? ›

To find the mean, add up the values in the data set and then divide by the number of values that you added. To find the median, list the values of the data set in numerical order and identify which value appears in the middle of the list.

How to find median in an even set of numbers in a frequency table? ›

If the number of terms is even, the median is the mean of the two middle numbers. Arrange the numbers in order by size. If the number of terms is odd, the median is the middle term. If the number of terms is even, add the two middlemost terms and then divide by 2.

## References

Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 5951

Rating: 4.8 / 5 (58 voted)

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.