Quick revision
The short answer
Walk up each employee's chain with a recursive CTE that starts at their direct manager and repeatedly joins the manager's manager, carrying the highest manager salary seen so far with GREATEST. Keep the final row of each chain, where the last manager has no manager, and return employees whose salary is above that maximum; because only rows reached by following manager_id are ever joined, peers and other branches are never compared.
What to remember
In this answer, we walked each employee's chain upward with a recursive CTE, carried the highest manager salary with GREATEST, kept only rows that reached the top and compared the employee's salary with that maximum. We noted how GREATEST treats NULLs differently in PostgreSQL and MySQL, saw an equivalent pair-based version with HAVING and MAX, and explained that following manager_id is what keeps peers and other branches out of the comparison.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Walk up each employee's chain with a recursive CTE that starts at their direct manager and repeatedly joins the manager's manager, carrying the highest manager salary seen so far with GREATEST. Keep the final row of each chain, where the last manager has no manager, and return employees whose salary is above that maximum; because only rows reached by following manager_id are ever joined, peers and other branches are never compared.
Background to this question
The direct-manager version of this question is a warm-up; this one tests whether we can carry state through a recursive query. It is also a nice example of a general technique: when a question says every ancestor, we either walk the chain while accumulating an aggregate, or build all employee-ancestor pairs and aggregate them. In this answer, we will recap walking a chain upward, write the accumulating recursive CTE, look at the pair-based alternative, explain why only the chain is compared, and add the guards a real hierarchy needs.
1A Quick Recap of Walking Up a Chain
Each employee row points to its manager through manager_id, and the top person has NULL. Walking upward means: start with the employee's manager, then that manager's manager, and so on until we reach someone whose manager_id is NULL. Every step is a primary key lookup, because we look up the row whose id equals the current manager_id.
Earning more than every manager above me is the same as earning more than the highest-paid of them. So instead of keeping all the managers, we only need to carry one number along the walk: the maximum manager salary seen so far.
2Carrying the Maximum Along the Chain
Here is a small company. Ann is the CEO, Bob reports to Ann, Cat to Bob, and Dan and Eve to Cat. Fay is another VP under Ann.
| Step | Manager reached | Max manager salary | next_mgr |
|---|---|---|---|
| anchor | Cat (150) | 150 | 2 (Bob) |
| 1 | Bob (200) | 200 | 1 (Ann) |
| 2 | Ann (300) | 300 | NULL |
The anchor gives one row per employee who has a manager, with that manager's salary as the starting maximum and the manager's own manager_id as the next step. Each recursive step joins the next manager, updates the maximum and moves next_mgr one level up. When next_mgr is NULL, the row describes the complete chain, so the final query keeps only those rows.
Dan earns 180, more than his direct manager Cat, so he appears in the simple version of this question. But Bob earns 200 and Ann 300, so the maximum over his chain is 300 and Dan is not returned. Eve earns 320, above all three, and is the only result.
3A Note on NULL Salaries and GREATEST
GREATEST behaves differently across databases. In PostgreSQL it ignores NULL arguments, so a manager with an unknown salary simply does not raise the maximum. In MySQL, GREATEST returns NULL if any argument is NULL, so one unknown salary makes the whole chain's maximum NULL and the final comparison unknown, which excludes the employee.
Neither is wrong, but they answer different questions. If unknown salaries should block the result, we can make that explicit in PostgreSQL by carrying a second column, has_unknown, computed at each step as u.has_unknown OR m.salary IS NULL, and excluding chains where it is true. If they should be ignored, we wrap the salary in COALESCE in MySQL. Saying which one we want is part of a strong answer.
4The Alternative: All Employee-Ancestor Pairs
Another way is to let the recursion produce every pair of an employee and one of their ancestors, and then aggregate. This is often easier to read, because the comparison is a plain HAVING.
Notice that MAX ignores NULL salaries, the same as PostgreSQL's GREATEST. The pair version produces more rows, one per employee per level, but those rows are useful in their own right: they are exactly what a closure table would store.
5Why Only the Chain Is Compared
Both queries only ever join a row reached by following manager_id from the employee. Fay earns 250, which is less than Eve's 320, but the question is irrelevant: Fay is not in Eve's chain, and neither query can reach her, because no step ever goes sideways to a peer or down to another branch. A tempting wrong answer compares against all managers in the company, for example everyone who has at least one report, which would wrongly bring Fay, or a rich manager in another division, into the comparison.
For real data we keep the guards from the recursive CTE habits: the depth limit stops a cycle from looping forever, and indexes on the primary key, which exists already, and on manager_id keep each step a lookup.
6Follow-up questions
How does this change if the hierarchy is stored as a closure table?
The recursion disappears. A closure table holds every ancestor-descendant pair, so we join employee to the closure on descendant, keep depth greater than 0 so the employee is not compared with themselves, join the ancestor's employee row and group: GROUP BY e.id, e.name, e.salary HAVING e.salary > MAX(a.salary). It is a single join and aggregate, fast with an index on (descendant, ancestor). Employees at the top have no ancestor rows and drop out naturally.
Why does the first query keep only rows where next_mgr is NULL?
Each recursive step adds a new row rather than replacing the previous one, so every partial chain is in the result. Only the row whose last manager has no manager has seen all the managers, and therefore the true maximum. If the chain was cut by the depth limit, there is no such row and the employee is left out, which is the safe outcome.
Is it expensive on a big company?
Each employee produces one row per level, so 100,000 employees with an average depth of 6 give about 600,000 rows, fine for a batch report. For frequent use, a closure table or a stored path makes it a single pass.
7Conclusion
In this answer, we walked each employee's chain upward with a recursive CTE, carried the highest manager salary with GREATEST, kept only rows that reached the top and compared the employee's salary with that maximum. We noted how GREATEST treats NULLs differently in PostgreSQL and MySQL, saw an equivalent pair-based version with HAVING and MAX, and explained that following manager_id is what keeps peers and other branches out of the comparison.
Open with
Earning more than every manager above you is the same as earning more than the highest-paid of them, so I walk up each chain and carry that maximum.
Then, if they want more
I use a recursive CTE. The anchor joins each employee to their direct manager and records the employee id, the employee's salary, the manager's salary as the starting maximum and the manager's manager_id as the next step.
The recursive member joins the employee row whose id is that next step, updates the maximum with GREATEST of the carried value and the new manager's salary, and moves next step up again.
When next step is NULL, the chain has reached the top, so that row has seen every manager; in the final query I keep only those rows and return employees whose salary is above the maximum.
An alternative is to generate all employee and ancestor pairs recursively, then group by employee with HAVING salary greater than MAX of the ancestors' salaries. The query only ever reaches rows by following manager_id, so peers and people in other branches are never compared.
I would add a depth limit against cycles, rely on the primary key for each step, and mention that GREATEST ignores NULLs in PostgreSQL but returns NULL in MySQL, so I would decide how unknown salaries should count.
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.
- Official documentation
- PostgreSQL documentation, The SQL Language
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 "Using employee(id, name, salary, manager_id), return employees who earn more than every...": https://scrivio-interview-library.netlify.app/databases/sql-coding-problems/bd628c40b6/