Rank departments by the z-score of their average salary against the company in SQL
Quick Overview
Write a SQL query that standardizes each department's average salary against the company-wide mean and sample standard deviation, then ranks departments by how far they deviate. It tests combining aggregates at two grains, window ranking on an absolute value, and the difference between sample and population standard deviation.
Rank departments by the z-score of their average salary against the company in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `employees` table has a `department` and a `salary` for each employee. For every department, compute `z_score` = (department_avg_salary - company_mean_salary) / company_std, where the company mean and the sample standard deviation are taken over all salaries in the table. Return one row per `department` with `department`, `avg_salary` (rounded to 2 decimals), `z_score` (rounded to 4 decimals) and `dept_rank`: `RANK()` by absolute `z_score` descending, so rank 1 is the department whose average deviates most from the company mean in either direction.
```hint Two levels of aggregation
The department averages and the company-wide figures are computed at different grains. Work them out separately, then bring them together.
```
### Input Table(s)
Table `employees`:
| Column | Type | Meaning |
|---|---|---|
| department | VARCHAR(30) | The employee's department |
| salary | NUMERIC(10,2) | The employee's salary |
There is no key. Each row is one employee, and several employees can have the same department and salary.
### Output Contract
- Columns, in this order: `department`, `avg_salary`, `z_score`, `dept_rank`.
- One row per distinct `department`.
- `avg_salary` is the department's mean salary, rounded to 2 decimals.
- `z_score` uses the company mean and the sample standard deviation (divisor n - 1) of every salary in the table, and is rounded to 4 decimals.
- `dept_rank` is `RANK()` over the unrounded absolute z-score, largest first. Departments with equal values share a rank and the next rank is skipped.
- Rows are sorted by `dept_rank` ascending, then `department` ascending.
### Example
Input:
```text
employees
+-------------+-----------+
| department | salary |
+-------------+-----------+
| sales | 65188.39 |
| engineering | 116680.98 |
| support | 52253.42 |
| marketing | 75412.96 |
| sales | 69300.07 |
| engineering | 106856.13 |
| engineering | 98977.61 |
| marketing | 87086.25 |
| sales | 81899.66 |
| support | 63682.83 |
| support | 48043.00 |
| support | 56828.79 |
| marketing | 70136.07 |
| sales | 87941.60 |
| support | 60324.64 |
+-------------+-----------+
```
Expected output:
```text
+-------------+------------+---------+-----------+
| department | avg_salary | z_score | dept_rank |
+-------------+------------+---------+-----------+
| engineering | 107504.91 | 1.5536 | 1 |
| support | 56226.54 | -0.9784 | 2 |
| marketing | 77545.09 | 0.0743 | 3 |
| sales | 76082.43 | 0.0021 | 4 |
+-------------+------------+---------+-----------+
```
The 15 salaries have a mean of 76040.83 and a sample standard deviation of about 20252.46. Engineering's average sits about 1.55 standard deviations above the mean, the largest gap, so it ranks first. Support is below the mean, but its absolute z-score is the second largest, so it ranks second.
### Constraints and Clarifications
- `department` and `salary` are never NULL.
- If the table has a single row, or every salary is the same, the standard deviation is NULL or 0. In that case `z_score` is NULL for every department and every department gets `dept_rank` 1.
- The company mean is the mean of all individual salaries, weighted by headcount. It is not the mean of the department averages.
- Values are compared as numbers, so trailing zeros do not matter.
Overview: Write a SQL query that standardizes each department's average salary against the company-wide mean and sample standard deviation, then ranks departments by how far they deviate. It tests combining aggregates at two grains, window ranking on an absolute value, and the difference between sample and population standard deviation.
The employees table has a department and a salary for each employee. For every department, compute z_score = (department_avg_salary - company_mean_salary) / company_std, where the company mean and the sample standard deviation are taken over all salaries in the table. Return one row per department with department, avg_salary (rounded to 2 decimals), z_score (rounded to 4 decimals) and dept_rank: RANK() by absolute z_score descending, so rank 1 is the department whose average deviates most from the company mean in either direction.
Input Table(s)
Table employees:
Column
Type
Meaning
department
VARCHAR(30)
The employee's department
salary
NUMERIC(10,2)
The employee's salary
There is no key. Each row is one employee, and several employees can have the same department and salary.
Output Contract
Columns, in this order:
department
,
avg_salary
,
z_score
,
dept_rank
.
One row per distinct
department
.
avg_salary
is the department's mean salary, rounded to 2 decimals.
z_score
uses the company mean and the sample standard deviation (divisor n - 1) of every salary in the table, and is rounded to 4 decimals.
dept_rank
is
RANK()
over the unrounded absolute z-score, largest first. Departments with equal values share a rank and the next rank is skipped.
Rows are sorted by
dept_rank
ascending, then
department
ascending.
The 15 salaries have a mean of 76040.83 and a sample standard deviation of about 20252.46. Engineering's average sits about 1.55 standard deviations above the mean, the largest gap, so it ranks first. Support is below the mean, but its absolute z-score is the second largest, so it ranks second.
Constraints and Clarifications
department
and
salary
are never NULL.
If the table has a single row, or every salary is the same, the standard deviation is NULL or 0. In that case
z_score
is NULL for every department and every department gets
dept_rank
1.
The company mean is the mean of all individual salaries, weighted by headcount. It is not the mean of the department averages.
Values are compared as numbers, so trailing zeros do not matter.