Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Table employees(id, name, manager_id, salary). Write SQL that lists employees who earn more than their manager, with both names.

We join the employees table to itself under two aliases, e for the employee and m for the manager, on m.id = e.manager_id, and keep rows where e.salary > m.salary: SELECT e.name, m.name AS manager FROM employees e JOIN employees m ON m.id = e.manager_id WHERE e.salary > m.salary. The inner join naturally excludes the person at the top, who has no manager. To list everyone with their manager's name, we use a LEFT JOIN, which gives NULL for the top.

Background to this question

Hierarchies stored in one table, where each row points to another row of the same table, are everywhere: employees and managers, categories and parent categories, comments and replies. A self join is the basic tool for comparing a row with its parent, and this question is the classic way to check that a candidate is comfortable treating one table as two. In this answer, we will recap what a self join is, write the query, follow how the aliases work, discuss the top of the hierarchy and NULLs, add the index that helps on large tables, and show the LEFT JOIN variant.

1A Quick Recap of Self Joins

A self join is an ordinary join where both sides are the same table. The table employees(id, name, manager_id, salary) stores each manager as an employee too, and manager_id holds the id of the employee's manager. To compare an employee with their manager, we need two rows of the same table side by side, so we list the table twice in FROM with different aliases. An alias is a short name for a table within one query; with two aliases, the database treats them as two independent copies, and we can refer to the employee's columns and the manager's columns separately.

2Writing the Query

Sample data: Mia is the CEO with no manager and a salary of 200,000. Raj reports to Mia and earns 150,000. Lea and Tom report to Raj and earn 160,000 and 120,000.

SQL
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;
-- Lea | 160000 | Raj | 150000
Pairs produced by the join before WHERE
e (employee)e.salarym (manager)m.salarye.salary > m.salary
Raj150,000Mia200,000false
Lea160,000Raj150,000true
Tom120,000Raj150,000false
Mia has manager_id NULL, so the inner join finds no manager row for her and she is not in the list.

Only Lea earns more than her manager. Raj earns less than Mia, Tom earns less than Raj, and Mia has no manager row to compare with.

3How the Aliases Work

Read the join condition m.id = e.manager_id from the employee's point of view: for each employee row e, find the row m whose id equals e's manager_id. That row is the manager. Getting the direction right is the main thing to watch. If we wrote e.id = m.manager_id instead, m would be the employee's report rather than their manager, and the query would find managers who earn more than someone who reports to them, nearly the opposite question.

Naming the aliases after their roles, e or emp and m or mgr, rather than t1 and t2, makes the direction obvious to the reader. Every column in the query must be qualified with an alias, because both copies have the same column names; an unqualified name would be ambiguous and the database would reject it.

4The Top of the Hierarchy and NULLs

Mia's manager_id is NULL. In the join condition m.id = NULL, the comparison is unknown for every row, so no manager row matches her, and the inner join drops her. That is exactly right for this question, because someone with no manager cannot earn more than their manager.

Sometimes the requirement is the opposite: list everyone with their manager's name, including the CEO. Then we use a LEFT JOIN, which keeps employees without a matching manager and fills the manager columns with NULL.

SQL
SELECT e.name AS employee,
COALESCE(m.name, '(no manager)') AS manager
FROM employees e
LEFT JOIN employees m ON m.id = e.manager_id
ORDER BY e.name;
-- Lea | Raj
-- Mia | (no manager)
-- Raj | Mia
-- Tom | Raj

If we combined this LEFT JOIN with WHERE e.salary > m.salary, Mia would disappear again, because the comparison with her NULL manager salary is unknown. For the original question, the inner join is the honest choice.

5Making It Fast on Large Tables

For each employee, the join looks up one manager by m.id, which is the primary key and already indexed, so this direction is cheap. Queries that go the other way, such as listing the direct reports of a manager, filter or join on manager_id, and for those an index on employees(manager_id) matters; without it, each lookup scans the whole table. Since the question mentions large tables, adding that index is worth it, and a foreign key from manager_id to id, which PostgreSQL does not index automatically, should have one anyway.

For a single comparison with the direct manager, a self join is all we need. Questions that walk the hierarchy to any depth, such as everyone in Raj's organisation, need a recursive CTE, which repeats the self join level by level.

SQL
WITH RECURSIVE org AS (
SELECT id, name FROM employees WHERE name = 'Raj'
UNION ALL
SELECT e.id, e.name FROM employees e JOIN org o ON e.manager_id = o.id
)
SELECT name FROM org; -- Raj, Lea, Tom

6Follow-up questions

Could this be written with a subquery instead?

Yes: SELECT e.name FROM employees e WHERE e.salary > (SELECT m.salary FROM employees m WHERE m.id = e.manager_id). The correlated subquery returns the manager's salary for each employee, or NULL for the CEO, which makes the comparison unknown and drops her. It is equivalent, but the join also gives us the manager's name easily, and planners usually execute both similarly.

What if an employee's manager_id points to an id that does not exist?

The inner join silently drops that employee. A foreign key constraint from manager_id to id prevents such orphans; without one, a LEFT JOIN with WHERE e.manager_id IS NOT NULL AND m.id IS NULL finds them.

How would you find employees who earn more than everyone above them, not just their direct manager?

Walk up the hierarchy with a recursive CTE that carries the maximum salary seen so far among each employee's managers, then compare the employee's salary with that maximum. That is a hierarchy query, beyond a single self join.

7Conclusion

In this answer, we compared each employee with their manager by joining the employees table to itself under two aliases, e and m, on m.id = e.manager_id, and filtering on e.salary > m.salary. The inner join excludes the person at the top, whose NULL manager_id matches nothing, while a LEFT JOIN lists everyone with NULL as the top manager. Naming aliases by role keeps the join direction clear, the manager lookup uses the primary key, and an index on manager_id helps the queries that go downwards.

What to say in the interviewAbout 90 seconds aloud

Open with

This is a self join. The table holds both employees and their managers, so I list it twice with two aliases: e for the employee and m for the manager, and join on m.id equals e.manager_id, which reads as for each employee, find the row whose id is that employee's manager id.

Then, if they want more

Then the WHERE keeps e.salary greater than m.salary, and I select e.name and m.name as manager. The direction of the join condition is the thing to get right; if I flipped it, m would be a direct report instead of the manager. I name the aliases by role and qualify every column, since both copies have the same column names.

Because it is an inner join, the person at the top, whose manager_id is NULL, has no matching manager row and drops out, which is correct here since they have no manager to out-earn.

If the requirement were to list every employee with their manager's name, I would use a LEFT JOIN, so the CEO appears with a NULL manager, and I could COALESCE that to something like no manager.

Performance-wise, each lookup of the manager goes through the primary key, so it is cheap; for large tables I would still index manager_id, because queries that go downwards, like listing someone's direct reports, filter on it, and PostgreSQL does not index foreign key columns automatically. For deeper hierarchy questions I would use a recursive CTE.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Table employees(id, name, manager_id, salary). Write SQL that lists employees who earn...": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/f6022034db/