Compare Each Employee's Salary with Their Manager's
Return each employee with the difference between their salary and their manager's, largest first.
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 employees with highest salary difference compared to manager.
A correct answer returns 110 rows with 2 columns: id, 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 | diff |
|---|---|
First 5 of 110 rows. This is the output of the model solution on the practice database.
What you practise
- Combining rows from two tables on a matching key with an inner join
- Joining a table to itself under two aliases
- Sorting the result with ORDER 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
Self join difference
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
employeestable, under the aliase. - Join
employeesagain, under the aliasm, one.manager_id=m.id. - Add the calculated column
diff:e.salary - m.salary. - Sort the result by
diffdescending.
Model solution
Checking your session…
What to watch for
The inner self join matches each employee to the row of their manager and drops the ten people who have none. A positive difference means the employee earns more than the manager.
- Self join. Without a condition such as a.id < b.id, a self join pairs every row with itself and lists each pair twice.
- INNER JOIN. An inner join drops rows that have no partner on the other side, and it repeats a row once for every match, which inflates SUM and COUNT.
- Calculated columns. Dividing one integer by another truncates the result in SQLite. Multiply one side by 1.0 to get decimals.
Related SQL challenges
- Find Managers Who Earn More Than All Their Direct ReportsSimilar levelHard
- Calculate Employee Tenure in Days and Sort by LongestSimilar levelHard
- Find Pairs of Employees in the Same DepartmentEasierMedium
- List Employees with Their Manager Names Using a Self JoinEasierMedium
- Find the 3 Largest Jumps Between Adjacent SalariesSimilar levelHard
Keep learning
- Lesson: SELF JOIN
- Lesson: INNER JOIN
- Lesson: Comments & Aliases
- Lesson: ORDER BY
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database