HardSQL challenge #312SELF JOINCALCULATIONORDER BY

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.

Solve This SQL Challenge

Why this question comes up

HR wants to find employees paid noticeably more than their own manager, which can signal a pay-band problem. Sorting by the difference puts those cases at the top.

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

First 3 rows of the employees table
idfirst_namelast_nameemaildepartmentdepartment_idtitlesalaryhire_datemanager_idcitystatus
1WilliamLewiswlewis18@corp.ioCustomer Success7CS Specialist617742022-07-2460San Diegoactive
2JohnMillerjmiller51@corp.ioLegal8Senior Counsel1916942021-08-0955San Diegoactive
3DanielRiveradrivera50@corp.ioEngineering1Staff Engineer1350672016-06-06106Houstonactive

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
iddiff
6366596
1164470
10247726
11246830
5737691

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:

  1. Read from the employees table, under the alias e.
  2. Join employees again, under the alias m, on e.manager_id = m.id.
  3. Add the calculated column diff: e.salary - m.salary.
  4. Sort the result by diff descending.
Show the model solution
SELECT e.id, e.salary - m.salary as diff
FROM employees e
JOIN employees m ON e.manager_id = m.id
ORDER BY diff DESC;

This query produced the expected output above. Yours does not have to match it word for word: any query that returns the same rows is correct.

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.
Solve This SQL Challenge

Keep learning