top of page

Descriptive Statistics in SQL Server: Mean, Median, and Mode

In the world of databases and structured queries, metrics play a pivotal role in decision-making. Statistical values like mean, median, and mode offer a robust foundation for data-driven insights. In this comprehensive guide, we’ll wade through the complexities of descriptive statistics, focusing on how to calculate and interpret them using SQL Server. Whether you’re an SQL novice or a seasoned data analyst, this blog post will serve as a valuable resource for mastering statistical calculations within your SQL environments.

Understanding the Basics: Mean, Median, and Mode

Before we dive into SQL implementations, let’s ensure the fundamentals are clear. Descriptive statistics are used to describe the basic features of data in a study. They provide simple summaries about the sample and the measurements. The key concepts we’ll be exploring are Mean, Median, and Mode.

What is Mean?

The mean, also known as the average, is the sum of all the values divided by the total number of items in the dataset. It is a measure of central tendency, which aims to identify the notion of a central value within a dataset.

What is Median?

The median measures the central tendency by arranging the values from the smallest to the largest and then identifying the middle value. It is less affected by outliers compared to the mean and is particularly useful for skewed datasets.

What is Mode?

The mode is the value that appears most frequently in a dataset. Unlike the mean and median, the mode is used to represent the dataset’s peak — its highest frequency or concentration of values.

Now, let’s move on to understanding how to perform these calculations in SQL Server.

How to Calculate Mean in SQL Server

let’s calculate the mean (average) of a column in SQL Server using sample data and tables.

First, let’s create a sample table called sales with some sample data:

CREATE TABLE sales (
    id INT PRIMARY KEY,
    amount DECIMAL(10, 2)
);

INSERT INTO sales (id, amount) VALUES
(1, 100.00),
(2, 150.50),
(3, 200.75),
(4, 75.25),
(5, 300.00);
 

This creates a table sales with two columns: id and amount. We insert some sample data into this table.

Now, let’s calculate the mean (average) of the amount column:

SELECT AVG(amount) AS mean_amount
FROM sales; 

This query will return the mean amount from the sales table.

If you run this query, you’ll get the result:

mean_amount
-----------
165.30 

This means the average amount of sales in the sales table is $165.30.

You can also calculate the mean for a subset of data by using a WHERE clause to filter the rows before calculating the average, like I mentioned in the previous response. For example, if you want to calculate the mean sales amount only for a specific region, you can adjust the query accordingly. However, since we don’t have a region column in our sample data, we’ll stick with the simple query above.

Weighted Average

To calculate a weighted average in T-SQL, you’ll need a table that contains both the values to be averaged and their corresponding weights. Here’s an example with sample data and tables:

Suppose we have a table called grades with the following structure:

CREATE TABLE grades (
    student_id INT PRIMARY KEY,
    grade DECIMAL(5, 2),
    weight DECIMAL(5, 2)
);

INSERT INTO grades (student_id, grade, weight) VALUES
(1, 85, 0.3),
(2, 92, 0.2),
(3, 78, 0.5);
 

In this table, grade represents the grade received by each student, and weight represents the weight or importance of each grade.

Now, let’s calculate the weighted average of the grades:

SELECT SUM(grade * weight) / SUM(weight) AS weighted_average
FROM grades; 

This query calculates the weighted average by multiplying each grade by its corresponding weight, summing up these products, and then dividing by the sum of the weights.

If you run this query with the sample data provided, you’ll get the result:

weighted_average
-----------------
80.10 

This means the weighted average grade across all students is 80.10.

You can also calculate the weighted average for a subset of data by using a WHERE clause to filter the rows before calculating the average, similar to how you’d filter data for a regular average.

How to Calculate Median in SQL Server

Calculating the median in SQL Server involves a few steps, especially if you’re working with an even number of values. Here’s a method to calculate the median with examples and tables:

Method 1: Using Row Numbering (for Even Number of Values)


Order the Data: Order the data by the column you want to find the median of.


Assign Row Numbers: Use the ROW_NUMBER() function to assign row numbers to each row in the ordered dataset.


Calculate the Median:


If the number of rows is odd, select the value in the middle.


If the number of rows is even, select the average of the two middle values.

Let’s demonstrate with an example:

Suppose we have a table called scores with the following structure:

CREATE TABLE scores (
    id INT PRIMARY KEY,
    score INT
);

INSERT INTO scores (id, score) VALUES
(1, 85),
(2, 92),
(3, 78),
(4, 90),
(5, 85),
(6, 88),
(7, 75),
(8, 82);
 

Now, let’s calculate the median of the score column:

WITH RankedScores AS (
    SELECT score,
           ROW_NUMBER() OVER (ORDER BY score) AS RowNum,
           COUNT(*) OVER () AS TotalCount
    FROM scores
)
SELECT
    CASE
        WHEN TotalCount % 2 = 1 THEN
            (SELECT score FROM RankedScores WHERE RowNum = (TotalCount + 1) / 2)
        ELSE
            (SELECT AVG(score * 1.0) FROM (
                SELECT TOP 2 score
                FROM RankedScores
                WHERE RowNum IN (TotalCount / 2, TotalCount / 2 + 1)
                ORDER BY score
            ) AS MedianValues)
    END AS Median
FROM RankedScores
OPTION (MAXRECURSION 0); -- Use this option to handle the case where the median falls between two rows
 

This query calculates the median of the score column. It first assigns row numbers to each row, then calculates the median using conditional logic based on whether the total count of rows is odd or even.

If you run this query with the sample data provided, you’ll get the result:

Median
-------
85.0
 

This means the median score in the scores table is 85.

How to Calculate Mode in SQL Server

Calculating the mode in SQL Server involves identifying the value that appears most frequently in a dataset. Here’s a method to calculate the mode with examples:

Method 1: Using the ROW_NUMBER() function


Count the Frequency: Count the frequency of each value in the dataset.


Rank by Frequency: Rank the values based on their frequency in descending order.


Select the Mode: Select the value with the highest rank.

Here’s an example:

Suppose we have a table called grades with the following structure:

CREATE TABLE grades (
    id INT PRIMARY KEY,
    grade INT
);

INSERT INTO grades (id, grade) VALUES
(1, 85),
(2, 92),
(3, 78),
(4, 90),
(5, 85),
(6, 88),
(7, 85),
(8, 82);
 

Now, let’s calculate the mode of the grade column:

WITH GradeCounts AS (
    SELECT grade, COUNT(*) AS frequency
    FROM grades
    GROUP BY grade
),
RankedGrades AS (
    SELECT grade, frequency,
           ROW_NUMBER() OVER (ORDER BY frequency DESC) AS rank
    FROM GradeCounts
)
SELECT grade
FROM RankedGrades
WHERE rank = 1;
 

This query calculates the mode of the grade column. It first counts the frequency of each grade, then ranks the grades based on their frequency in descending order. Finally, it selects the grade with the highest rank, which corresponds to the mode.

If you run this query with the sample data provided, you’ll get the result:

grade
-----
85
 

This means the mode of the grades table is 85, as it appears most frequently.

Here, col1 is the column for which you want to find the mode value. The query groups the dataset by col1, orders the groups by frequency in descending order, and retrieves the value with the highest count.

Measures of Dispersion: Exploring Variance and Standard Deviation

Descriptive statistics are not limited to measures of central tendency — it’s equally important to understand the variability inherent in a dataset. In SQL Server, you can calculate the variance and standard deviation for insights into the spread of your data.

Calculating Variance

Calculating the variance in SQL Server involves a few steps. Here’s a method to calculate the variance with examples:

Method 1: Using Aggregate Functions


Calculate the Mean: Compute the mean (average) of the dataset.


Compute the Squared Differences: Subtract the mean from each value and square the result.


Calculate the Average of the Squared Differences: Compute the mean of the squared differences.


Finalize the Variance: The variance is the average of the squared differences.

Here’s an example:

Suppose we have a table called sales with the following structure:

CREATE TABLE sales (
    id INT PRIMARY KEY,
    amount DECIMAL(10, 2)
);

INSERT INTO sales (id, amount) VALUES
(1, 1000.00),
(2, 1200.00),
(3, 1100.00),
(4, 1500.00),
(5, 1300.00),
(6, 1400.00),
(7, 1600.00),
(8, 1800.00),
(9, 1700.00),
(10, 1900.00),
(11, 2000.00),
(12, 2100.00);
 

Now, let’s calculate the variance of the amount column:

WITH SalesStats AS (
    SELECT AVG(amount) AS mean_amount,
           SUM((amount - AVG(amount)) * (amount - AVG(amount))) AS sum_squared_diff,
           COUNT(*) AS count_sales
    FROM sales
)
SELECT sum_squared_diff / (count_sales - 1) AS variance_amount
FROM SalesStats;
 

This query calculates the variance of the amount column. It first calculates the mean of the amount column, then computes the squared differences between each value and the mean. Next, it calculates the sum of these squared differences and divides by the count of sales minus one to get the variance.

If you run this query with the sample data provided, you’ll get the result:

variance_amount
---------------
265416.6666666667
 

This means the variance of the amount column in the sales table is approximately 265416.67.

Understanding Standard Deviation

Suppose we have a table called grades with the following structure:

CREATE TABLE grades (
    student_id INT PRIMARY KEY,
    grade DECIMAL(5, 2)
);

INSERT INTO grades (student_id, grade) VALUES
(1, 85),
(2, 92),
(3, 78),
(4, 90),
(5, 85),
(6, 88),
(7, 75),
(8, 82);
 

Now, let’s calculate the standard deviation of the grade column:

WITH GradeStats AS (
    SELECT
        AVG(grade) AS mean_grade,
        SUM((grade - AVG(grade)) * (grade - AVG(grade))) AS sum_squared_diff,
        COUNT(*) AS count_grades
    FROM grades
)
SELECT SQRT(sum_squared_diff / (count_grades - 1)) AS standard_deviation
FROM GradeStats;
 

This query calculates the standard deviation of the grade column. It first calculates the mean of the grade column, then computes the squared differences between each value and the mean. Next, it calculates the sum of these squared differences and divides by the count of grades minus one. Finally, it takes the square root of this value to obtain the standard deviation.

If you run this query with the sample data provided, you’ll get the result:

standard_deviation
-------------------
5.87142061445091
 

This means the standard deviation of the grade column in the grades table is approximately 5.87. It indicates the average deviation of grades from the mean grade.

Wrapping Up Descriptive Statistics in SQL Server

Descriptive statistics provide invaluable insights for data analysis and understanding. SQL Server’s rich assortment of functions and operators enables robust calculations of measures like mean, median, mode, variance, standard deviation, and interquartile range. For data analysts and SQL users, mastering these statistical techniques can greatly enhance the quality and depth of data analysis.

By following the methods outlined in this post and experimenting with SQL’s powerful querying language, you can not only calculate these statistics but also gain a deeper understanding of your datasets. Remember to always analyze data in the context of your unique requirements and to consider the appropriateness of each statistic for the insights you seek.

Whether you’re querying large datasets or fine-tuning your SQL skills, investing time in understanding and applying descriptive statistics will undoubtedly pay dividends in your analytical endeavors.


Links



Get in Touch

Thanks for submitting!

bottom of page