Interview Drills · Self-join
26. Earning more than their manager
Concept: Self-joinshow
A self-join joins a table to itself under two aliases, so one row can be compared with a related row in the same table. For a manager hierarchy: FROM employees e JOIN employees m ON m.id = e.manager_id pairs every employee with their manager.
Example (different data):
SELECT c.name AS category, p.name AS parent FROM categories c JOIN categories p ON p.id = c.parent_id;
Your task
LeetCode-famous: comp review wants every case where someone out-earns the person they report to.
Return employee, employee_salary, manager and manager_salary for every employee who earns more than their manager. Sort by employee.
Starting Postgres in your browser…
Checking your access…