Self Joins

Self Joins

March 28, 2025

A SQL self join joins a table with itself. For example given the employee table below, write a SQL query that finds out employees who earn more than their managers. For the table, Joe is the only employee who earns more than his manager.

+----+-------+--------+------------+
| id | name  | salary | manager_id |
+----+-------+--------+------------+
| 1  | joe   | 70000  | 3          |
| 2  | henry | 80000  | 4          |
| 3  | sam   | 60000  | NULL       |
| 4  | max   | 90000  | NULL       |
+----+-------+--------+------------+

Answer:

SELECT a.name AS employee
FROM employee AS a
JOIN employee AS b on a.manager_id = b.id
WHERE a.salary> b.salary