Calculate a Z-Score for Each Salary
Return every employee with the z-score of their salary.
The challenge runs on the SQLabHub practice database (SQLite) and uses the employees table. You can solve it in the browser, with nothing to install.
The task
Calculate z-score of each employee salary.
A correct answer returns 120 rows with 13 columns: id, first_name, last_name, email, department, department_id, title, salary, hire_date, manager_id, city, status, z_score.
Tables used in this challenge
employees 120 rows
id INTEGER (PK) · first_name TEXT · last_name TEXT · email TEXT · department TEXT · department_id INTEGER · title TEXT · salary REAL · hire_date TEXT · manager_id INTEGER · city TEXT · status TEXT
| id | first_name | last_name | department | department_id | title | salary | hire_date | manager_id | city | status | |
|---|---|---|---|---|---|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| id | first_name | last_name | department | department_id | title | salary | hire_date | manager_id | city | status | z_score | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
First 5 of 120 rows. This is the output of the model solution on the practice database. The task does not fix a sort order, so your rows may come back in a different order.
What you practise
- Attaching a total or average to every row with an aggregate OVER ()
- Deriving a new column from an expression and naming it with an alias
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
Use AVG and STDDEV approximation
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
employeestable. - Compute
AVG(salary)with an emptyOVER (), so the figure for the whole result is repeated on every row. - Compute
AVG(salary * salary)with an emptyOVER (), so the figure for the whole result is repeated on every row. - Combine those values into
z_score:(salary - AVG(salary) OVER ()) / SQRT(AVG(salary * salary) OVER () - AVG(salary) OVER () * AVG(salary) OVER ()).
Model solution
Checking your session…
What to watch for
A z-score is (value - mean) / standard deviation. SQLite has no STDDEV, so the deviation is the square root of the mean of the squares minus the square of the mean, with each part computed as a window over the whole table.
- Window aggregates. A window aggregate keeps every row, while a plain aggregate collapses them. Mixing the two in one SELECT without GROUP BY is an error.
- Calculated columns. Dividing one integer by another truncates the result in SQLite. Multiply one side by 1.0 to get decimals.
Related SQL challenges
- Subtract the Overall Average from Each Salary with AVG() OVER ()Similar levelHard
- Calculate Each Order's Percentage of Total RevenueSimilar levelHard
- Show Each Salary's Gap to Its Department AverageSimilar levelHard
- Percent Difference of Each Salary from the Department AverageSimilar levelHard
- Calculate the Cumulative Percentage of RevenueSimilar levelHard
Keep learning
- Lesson: Window Functions
- Lesson: Comments & Aliases
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database