Calculating the mean, median, and mode in Oracle SQL is a task often performed by report developers or anyone else working with SQL. Learn how to calculate these three values from a range of data in this article.
What is the Mean, Median, and Mode, and Standard Deviation?
Mean, median, and mode are three kinds of average values that can be obtained from a set of data.
You might have learnt about them in high school maths (like I did) but may have forgotten what they are (like I did).
- Mean: This is the “average” that you might know. It’s the sum of all of the numbers divided by the count of numbers.
- Median: This is the middle value of a list of numbers.
- Mode: This is the value that occurs most often
- Standard Deviation: A value that represents how much numbers differ from each other
How Can I Calculate the Mean in Oracle SQL?
Let’s say we had a table of data called movie_ratings that looked like this:
How could we find the mean of these values?
We can use the AVG function.
SELECT AVG(rating) AS mean_rating FROM movie_ratings;
You can see the average is 14.75. This is because we added all of the numbers up (which equals 118) and then divided by the count of numbers (which is 8). 118/8 = 14.75.
How Can I Calculate the 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.
So, for example, you had these five values:
10, 15, 18, 23, 40.
The median of these would be the middle value after they have been ordered in ascending order. So, the median would be 18.
Median Function Parameters
The parameters of the MEDIAN function are:
- expr (mandatory): The expression to calculate a median for. This can be a set of numbers, or a column.
- query_partition_clause (optional): The clause that is used to partition the data when using MEDIAN as an analytic query.
The expr value can be any numeric data type. The MEDIAN function returns the same data type as the expr value.
If you specify the OVER clause, Oracle will work out the data type with the highest precedence and return that type.
Finding the Median
The MEDIAN function looks like this:
SELECT MEDIAN(rating) AS median_rating FROM movie_ratings;
You can see that the median is 14. This is because the middle value would be 14.
While there are 8 values and there is no middle value, the median is calculated as the point between the two middle values.
So, if we order the values they would be:
11, 12, 12, 13, 15, 16, 19, 20
The middle two values are 13 and 15, and the mid point between them is 14.
How Can I Calculate the Mode in Oracle SQL?
To calculate the mode, we need to use the STATS_MODE function.
SELECT STATS_MODE(rating) AS mode_rating FROM movie_ratings;
The mode is 12 as that is the most occurring value in the table.
Show the Mean, Median and Mode All In One Query
Let’s use these examples to show the mean, median, and mode all in the same query.
SELECT AVG(rating) AS mean_rating, MEDIAN(rating) AS median_rating, STATS_MODE(rating) AS mode_rating FROM movie_ratings;
As you can see, the values here are the same as the earlier examples.
So, that’s how you find the mean, median, and mode in Oracle SQL.
Examples of the MEDIAN Function
Here are some further examples of the MEDIAN function. I find that examples are the best way for me to learn about code, even with the explanation above.
Example 1 – aggregate
This example finds the median using it as an aggregate function.
SELECT MEDIAN(fees_required) AS median_fees_req FROM student;
It shows the value of 300 because this is the middle value in the column after it has been ordered.
Example 2 – analytic
This example uses the MEDIAN function as an analytic function
SELECT first_name, last_name, address_state, fees_required, MEDIAN(fees_required) OVER (PARTITION BY address_state) AS median_fees_req FROM student;
This example shows the median fees_required when grouped by the home_state for each record in the table.
How to Find the Standard Deviation in Oracle
Another statistic you may want to find is the standard deviation.
This can be achieved with the STDDEV function.
The purpose of the STDDEV function is to find the standard deviation of a set of numbers.
The standard deviation is a mathematical concept, which is a number that represents how much the values in a group differ from the mean value in a group.
This function can be used as either an analytical function or an aggregate function.
Oracle STDDEV Function Syntax and Parameters
The syntax for the STDDEV function when it is used as an aggregate function is:
STDDEV ( [DISTINCT | ALL] expression )
Alternatively, the syntax when used as an analytical function is:
STDDEV ( [DISTINCT | ALL] expression ) [OVER (analytical_clause) ]
The parameters of the STDDEV function as an aggregate function are:
- DISTINCT|ALL (optional): Using DISTINCT means that the function will only look at unique values. Using ALL means that all values will be considered.
- expression (mandatory): The expression is the set of data or the column that is used in the STDDEV function.
The parameters of the STDDEV function as an analytical function are the same, but also include:
- analytical_clause (optional): This is the clause or logic that is used to group the values for the STDDEV function.
Examples of the STDDEV Function
Here are some examples of the STDDEV function. I find that examples are the best way for me to learn about code, even with the explanation above.
This example uses the STDDEV funiotn as an aggregate function.
SELECT STDDEV(fees_required) FROM student;
This example uses the STDDEV function as an aggregate function again, but I’ve used the DISTINCT keyword.
SELECT STDDEV(DISTINCT fees_required) FROM student;
This example uses the STDDEV function as an analytical function.
SELECT enrolment_date, TO_CHAR(enrolment_date, 'MON') AS enrolment_mth, STDDEV(fees_required) OVER (order by TO_CHAR(enrolment_date, 'MON')) AS stdev_val FROM student;
You can see that for each of the enrolment_mth values that are the same, the stdev_val is the same.
So, that’s how you find the mean, median, mode, and standard deviation in Oracle SQL.
Lastly, if you enjoy the information and career advice I’ve been providing, sign up to my newsletter below to stay up-to-date on my articles. You’ll also receive a fantastic bonus. Thanks!