← Back to SQL, page 3

sql-0052 · Medium mid

Write a query to find employees who earn more than their own manager.

Join the employees table to itself: one copy for the employee (e) and one for the manager (m), matched on e.manager_id = m.id. Then compare e.salary > m.salary. Employees with no manager drop out of the inner join, which is correct here because they have no manager to compare against.

Full answer

A self join treats one table as two. Each copy gets its own alias, and the join condition links a row to another row in the same table.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

The data already has an interesting case. Sara reports to Tom, and Omar reports to Diego. Let's see who out-earns their manager:

sql
SELECT e.name AS employee, e.salary, m.name AS manager, m.salary AS manager_salary
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.salary > m.salary
ORDER BY e.name;
employeesalarymanagermanager_salary

(0 rows)

Nobody does, with the original salaries. Give Omar a raise and run it again:

sql
UPDATE employees SET salary = 60000 WHERE name = 'Omar';
SELECT e.name AS employee, e.salary, m.name AS manager, m.salary AS manager_salary
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.salary > m.salary
ORDER BY e.name;
employeesalarymanagermanager_salary
Omar60000Diego55000

How the query works:

  • employees e is "the person", employees m is "their manager".
  • ON m.id = e.manager_id pairs each person with the row of their manager.
  • WHERE e.salary > m.salary keeps the pairs where the person earns more.

Asha has no manager, so the inner join finds no partner for her and she is excluded, which is what the question wants. Lena's salary is NULL, so her comparison is UNKNOWN and she is excluded too. If the question were "employees with no manager or earning more than their manager", you would switch to a LEFT JOIN and add OR m.id IS NULL.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT e.name AS employee, e.salary, m.name AS manager, m.salary AS manager_salary
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.salary > m.salary
ORDER BY e.name;

UPDATE employees SET salary = 60000 WHERE name = 'Omar';
SELECT e.name AS employee, e.salary, m.name AS manager, m.salary AS manager_salary
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.salary > m.salary
ORDER BY e.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • A self join uses two aliases for the same table
  • Join on employee.manager_id = manager.id
  • Inner join drops people without a manager
  • NULL salaries make the comparison UNKNOWN and drop the row

Common mistakes

  • Using one alias and comparing a row with itself
  • Reversing the join condition and comparing managers with their reports the wrong way round

Follow-ups

  • How would you list each manager with the number of direct reports?
  • How would you find the full chain of managers above an employee?

Tips

  • Say which alias plays which role before writing the condition