Write SQL to list each employee with their manager's name (including people without a manager), then find employees who earn more than their manager.
Join the employees table to itself with aliases e and m on m.id = e.manager_id; LEFT JOIN keeps top-level employees with a NULL manager. For "earns more than the manager" use an inner self-join with WHERE e.salary > m.salary.
Self-join idea
A self-join joins a table to itself using two aliases, one for the employee and one for the manager. The table has employees(id, name, salary, manager_id) where manager_id points to another row in the same table.
Query 1: every employee with the manager's name
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON m.id = e.manager_id;
The CEO has manager_id = NULL. An inner join would drop that row; the LEFT JOIN keeps it with manager = NULL. Use COALESCE(m.name, 'No manager') for a friendly label.
Sample data and result (I ran this logic in SQLite):
| employee | manager |
|---|---|
| CEO | NULL |
| Asha | CEO |
| Ravi | CEO |
| Meena | Asha |
Query 2: employees who earn more than 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;
An inner join is right here: someone with no manager has nothing to compare with, and NULL comparisons would be UNKNOWN anyway, so those rows would not be returned.
Common traps
- Forgetting aliases: both sides are the same table, so every column needs
e.orm.. - Joining on the wrong column:
m.id = e.manager_id(the manager's id equals the employee's manager_id), note.id = m.id. - Putting a filter on
min theWHEREclause of aLEFT JOIN, which turns it into an inner join. Put such conditions in theONclause.
Performance and limits
Index manager_id so the join can look up managers quickly. A self-join shows only one level (direct manager). For the whole chain of command (manager of manager) use a recursive CTE.
- 1employees eeach employee
- 2LEFT JOIN employees mON m.id = e.manager_id
- 3Top levelmanager columns NULL, still listed
- 4Inner join + WHEREe.salary > m.salary
See the concept: Join fan-outWhy a join to a one-to-many table multiplies sums
| O101 | line 1 | ₹300 |
| O101 | P101 | FAILED |
| O101 | P102 | SUCCESS |
| line 1 | P101 | ₹300 |
| line 1 | P102 | ₹300 |
Wrong: the single ₹300 line is counted once per payment attempt.
Fix: aggregate payments to one row per order first
line 1, paid, ₹300, so SUM = ₹300
Key points
- A self-join uses two aliases of the same table
- LEFT JOIN keeps employees whose manager_id is NULL
- Join condition is m.id = e.manager_id
- Use an inner join with WHERE e.salary > m.salary for the comparison
- Only one level of hierarchy; use a recursive CTE for more
Common mistakes
- Using an inner join for query 1 and losing the top-level employee
- Joining on the wrong columns so each employee is its own manager
- Filtering on the manager table in WHERE after a LEFT JOIN
- Forgetting to qualify column names with the alias
Prerequisites
- INNER JOIN and LEFT JOIN
- Table aliases
Best practices
- Use descriptive aliases such as emp and mgr
- Index the manager_id column
- Use COALESCE to show a label for missing managers
Likely follow-ups
- How would you find the employees with no one reporting to them?
- How would you list the full chain of managers for an employee?
- How would you find the highest-paid employee in each department using a window function?
- What is the difference between LEFT JOIN and INNER JOIN in a self-join?