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.
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:
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;| employee | salary | manager | manager_salary |
|---|
(0 rows)
Nobody does, with the original salaries. Give Omar a raise and run it again:
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;| employee | salary | manager | manager_salary |
|---|---|---|---|
| Omar | 60000 | Diego | 55000 |
How the query works:
employees eis "the person",employees mis "their manager".ON m.id = e.manager_idpairs each person with the row of their manager.WHERE e.salary > m.salarykeeps 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.
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