Employees Earning More Than Their Managers
EasyLeetCodeSQL: Joins
Hint and exampleShowHide
Join the table to itself, once in the role of the person and once in the role of their leader, then compare the two rows.
Example: players(id, name, goals, captain_id) — who scored more than their captain?
id | name | goals | captain_id
1 | Rao | 5 | NULL
2 | Asha | 8 | 1
3 | Ravi | 3 | 1
SELECT p.name
FROM players p
JOIN players c ON p.captain_id = c.id
WHERE p.goals > c.goals; -- returns Asha (8 > 5)