In this lesson, you will learn how to use Z-Score normalization to compare numeric data points across different groups that exist on entirely different scales.
The Problem: Apples vs. Oranges
Imagine you are analyzing performance across different retail departments. Department A tracks "Customer Satisfaction" on a scale of 1–5, while Department B tracks "Total Sales" in the thousands of dollars. If you try to compare a score of "4" in satisfaction directly against "4,000" in sales, your analysis will be meaningless. Z-Score normalization solves this by converting every value into the number of standard deviations it sits away from its group's average.
The Formula
To calculate a Z-Score, we use this formula for every row:
Z = (Value - Group_Average) / Group_Standard_Deviation
A Z-Score of 0 means the value is exactly average. A positive score means it’s above average, and a negative score means it’s below. This "unit-less" score allows you to compare performance regardless of the original scale.
Implementing in SQL
To calculate this, we use Window Functions to get the AVG() and STDDEV() for each group simultaneously:
SELECT
department,
value,
(value - AVG(value) OVER(PARTITION BY department)) /
STDDEV(value) OVER(PARTITION BY department) AS z_score
FROM department_metrics;
In this query, PARTITION BY department ensures that the average and standard deviation are calculated independently for each group. By subtracting the mean and dividing by the standard deviation, we effectively "re-center" all our data around zero. Now, a Z-Score of 1.5 in Department A means the exact same thing as a Z-Score of 1.5 in Department B: both represent performance that is 1.5 standard deviations above their respective group's mean.
This technique is vital when you need to identify outliers or compare relative performance in disparate datasets without being distracted by the raw magnitude of the numbers.
Key Takeaway: Z-Score normalization standardizes disparate datasets by centering them around zero and scaling them by their standard deviation, allowing for direct comparison across different units of measurement.