HardSQL challenge #342WINDOW FUNCTIONSTATISTICSANALYTICS

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.

Solve This SQL Challenge

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

First 3 rows of the employees table
idfirst_namelast_nameemaildepartmentdepartment_idtitlesalaryhire_datemanager_idcitystatus
1WilliamLewiswlewis18@corp.ioCustomer Success7Senior CS Manager617742022-07-24NULLSan Diegoactive
2JohnMillerjmiller51@corp.ioLegal8Paralegal1916942021-08-09NULLSan Diegoactive
3DanielRiveradrivera50@corp.ioEngineering1VP Engineering1350672016-06-06NULLHoustonactive

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
idfirst_namelast_nameemaildepartmentdepartment_idtitlesalaryhire_datemanager_idcitystatusz_score
1WilliamLewiswlewis18@corp.ioCustomer Success7Senior CS Manager617742022-07-24NULLSan Diegoactive-1.34015403562811
2JohnMillerjmiller51@corp.ioLegal8Paralegal1916942021-08-09NULLSan Diegoactive1.4413411272743433
3DanielRiveradrivera50@corp.ioEngineering1VP Engineering1350672016-06-06NULLHoustonactive0.2289971726124188
4MelissaSmithmsmith33@corp.ioEngineering1VP Engineering514652022-04-13NULLFort Worthactive-1.5608624225920986
5JasonMitchelljmitchell78@corp.ioDesign10Product Designer1568652019-10-17NULLDenveractive0.695676910612324

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:

  1. Read from the employees table.
  2. Compute AVG(salary) with an empty OVER (), so the figure for the whole result is repeated on every row.
  3. Compute AVG(salary * salary) with an empty OVER (), so the figure for the whole result is repeated on every row.
  4. 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.
Solve This SQL Challenge

Keep learning