Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

Table employee(id int primary key, name text, salary numeric, manager_id int null references employee(id)). Return the names of employees who earn more than their direct manager, with both salaries. What happens to employees without a manager or with a NULL salary?

Join the employee table to itself, once as the employee and once as the manager: SELECT e.name, e.salary, m.name AS manager, m.salary AS manager_salary FROM employee e JOIN employee m ON e.manager_id = m.id WHERE e.salary > m.salary. The inner join drops employees without a manager, which is right because there is nothing to compare, and a NULL salary on either side makes the comparison unknown, so those rows are excluded too.

Why this question is asked

This is one of the most common SQL screening questions, and it looks easy. Interviewers use it to check one idea, the self join, and then probe whether we understand what the query does with missing data, which is where real bugs come from. In this answer, we will recap what a self join is, write and run the query on a small data set, look at what happens to employees with no manager and with NULL salaries, and finish with indexing and a per-department variant.

1A Quick Recap of the Self Join

The manager of an employee is just another row of the same employee table, found through manager_id. To compare an employee with their manager in one row of output, we need both rows side by side. A self join does that: we list the same table twice in FROM under two aliases, here e for the employee role and m for the manager role, and join them on e.manager_id = m.id.

The database treats the two aliases as if they were two separate tables with the same data. There is nothing special about it beyond the naming, which is why clear aliases matter so much for readability.

2Writing and Running the Query

Here is a small data set and the query.

SQL
-- id | name | salary | manager_id
-- 1 | Ana | 9000 | NULL
-- 2 | Ben | 7000 | 1
-- 3 | Chen | 7500 | 2
-- 4 | Dana | 6000 | 2
-- 5 | Eli | NULL | 2
SELECT e.name, e.salary, m.name AS manager, m.salary AS manager_salary
FROM employee e
JOIN employee m ON e.manager_id = m.id
WHERE e.salary > m.salary;
-- Chen | 7500 | Ben | 7000
The joined rows before WHERE
e.namee.salarym.namem.salarye.salary > m.salary
Ben7000Ana9000false
Chen7500Ben7000true
Dana6000Ben7000false
EliNULLBen7000unknown
Ana has no row: her manager_id is NULL, so the inner join finds no manager for her.

For each employee row, the join finds the single manager row whose id matches its manager_id, because id is the primary key. The WHERE clause then compares the two salaries. Chen earns 7500 against Ben's 7000 and is returned. Dana earns less than Ben, so she is filtered out.

3Employees Without a Manager

Ana has manager_id NULL. The join condition e.manager_id = m.id is never true for her, because NULL compared with anything is unknown, so the inner join produces no row for Ana at all. Here that is the correct behaviour: the question asks for employees who earn more than their manager, and someone without a manager cannot satisfy it.

If the report should list every employee with a flag, we would switch to a LEFT JOIN, which keeps Ana with NULL manager columns, and move the comparison into a CASE in the SELECT list instead of WHERE. Putting e.salary > m.salary in WHERE after a LEFT JOIN would filter Ana out again, because the comparison with NULL is not true.

4NULL Salaries

Eli's salary is NULL. In SQL, NULL means unknown, and any comparison with it, NULL > 7000 included, is unknown rather than true or false. WHERE keeps only rows where the condition is true, so Eli is excluded, and the same would happen if Ben's salary were NULL: none of his reports could be returned.

Is that intended? Usually yes, since we cannot claim someone earns more when we do not know their pay. But it is worth saying in the interview, and in a real report we might list unknown salaries separately so HR can fix the data.

SQL
-- employees we could not compare because a salary is missing
SELECT e.name, e.salary, m.name AS manager, m.salary AS manager_salary
FROM employee e
JOIN employee m ON e.manager_id = m.id
WHERE e.salary IS NULL OR m.salary IS NULL;
-- Eli | NULL | Ben | 7000

5Indexing and Performance

The join looks up each employee's manager by m.id, the primary key, so every lookup uses the primary key index and the query is efficient. On a large table PostgreSQL will often choose a hash join instead, reading the table twice, which is also fine.

An index on manager_id helps the reverse question, who reports to X, which comes up in the next query someone writes, such as listing a manager's team. PostgreSQL does not create indexes on foreign key columns automatically, so we add it ourselves: CREATE INDEX ON employee (manager_id).

6Follow-up questions

How would you count such employees per department?

Assuming employee has a dept_id, keep the same self join and filter, then group: SELECT e.dept_id, COUNT(*) FROM employee e JOIN employee m ON e.manager_id = m.id WHERE e.salary > m.salary GROUP BY e.dept_id. Group by the employee's department, not the manager's, since managers can sit in a different department. To show departments with zero such employees, start from the department table and LEFT JOIN this result, using COALESCE(count, 0).

Could you write it with a subquery instead of a join?

Yes: SELECT name FROM employee e WHERE salary > (SELECT salary FROM employee m WHERE m.id = e.manager_id). It returns the same names, but showing the manager's name and salary requires the join anyway, and most planners turn the correlated subquery into a join.

What if an employee's manager_id points to a deleted row?

The inner join silently drops them, which can hide data errors. A foreign key from manager_id to employee(id) prevents such orphans, and that is why the table definition in the question has one.

7Conclusion

In this answer, we used a self join, the employee table under two aliases for the employee and manager roles, to compare each person's salary with their direct manager's. The inner join correctly drops people with no manager, NULL salaries make the comparison unknown so those rows are excluded, and the primary key makes the manager lookup efficient while an index on manager_id serves the reverse question.

What to say in the interviewAbout 90 seconds aloud

Open with

This is a self join. I use the employee table twice, alias e for the employee and m for the manager, joined on e.manager_id equals m.id, and filter where e.salary is greater than m.salary.

Then, if they want more

In the select list I return e.name and e.salary plus m.name and m.salary, so both salaries are shown. For each employee, the join finds exactly one manager row because m.id is the primary key. On the edge cases: an employee whose manager_id is NULL, like the CEO, gets no match in an inner join, so they are dropped.

That is correct here because there is no manager to compare with; if the report had to list everyone I would use a LEFT JOIN and compute the comparison in a CASE in the select list rather than in WHERE.

A NULL salary on either side makes the comparison unknown, and WHERE keeps only true rows, so those employees are excluded too. I would say explicitly whether that is intended, and I might list them separately so HR can fix the data. On performance, the manager lookup uses the primary key index.

An index on manager_id is not needed for this query but helps the reverse question, who reports to a given manager, and PostgreSQL does not create it automatically for the foreign key.

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 employee(id int primary key, name text, salary numeric, manager_id int null...": https://scrivio-interview-library.netlify.app/databases/sql-coding-problems/48c1920c78/