Show Each Salary's Gap to Its Department Average
Return every employee with how far their salary sits above or below 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.
The task
Find difference between salary and 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, 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
| 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 | diff | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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
AVG OVER partition
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)as a window function over eachdepartment, so the group's value is repeated on every one of its rows. - Combine those values into
diff:salary - AVG(salary) OVER (PARTITION BY department).
Model solution
Checking your session…
What to watch for
AVG(salary) OVER (PARTITION BY department) attaches the department average to each employee row. The differences inside a department sum to zero.
- 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.
Related SQL challenges
- Percent Difference of Each Salary from the Department AverageSimilar levelHard
- Subtract the Overall Average from Each Salary with AVG() OVER ()Similar levelHard
- Calculate the Average Salary per DepartmentEasierEasy
- Calculate a Z-Score for Each SalarySimilar levelHard
- Calculate Each Order Line's Share of Daily SalesSimilar 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