HardSQL challenge #382WINDOW FUNCTIONPARTITIONCALCULATION

Percent Difference of Each Salary from the Department Average

Return every employee with how far, in percent, their salary is from their department's average.

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 percentage difference of employee salary from department average.

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, pct_diff.

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_idcitystatuspct_diff
1WilliamLewiswlewis18@corp.ioCustomer Success7Senior CS Manager617742022-07-24NULLSan Diegoactive-53.948091023354216
15BarbaraBakerbbaker4@corp.ioCustomer Success7Senior CS Manager1882802018-12-158Philadelphiaon_leave40.360886815211394
18RobertGreenrgreen61@corp.ioCustomer Success7CS Manager731182023-08-049San Diegoactive-45.49125067901728
20EdwardRamirezeramirez83@corp.ioCustomer Success7Senior CS Manager1377372016-11-109Indianapolisactive2.6815777951283812
21CharlesAllencallen32@corp.ioCustomer Success7CS Director1629012023-02-148Houstonactive21.44109211326084

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 ()
  • Restarting a window calculation for each group with PARTITION BY
  • 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

salary - avg / avg

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) as a window function over each department, so the group's value is repeated on every one of its rows.
  3. Combine those values into pct_diff: (salary - AVG(salary) OVER (PARTITION BY department)) * 100.0 / AVG(salary) OVER (PARTITION BY department).

Model solution

Checking your session…

What to watch for

The formula is (salary - department average) * 100.0 / department average, with the average supplied by a partitioned window. Positive means above the department average.

  • 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.
  • PARTITION BY. PARTITION BY is not GROUP BY: it keeps every row and only decides which rows a window function looks at.
  • 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

Same technique, other tables

More practice on this pattern with different columns:

Keep learning