Skip to content
BytePatterns

Paid More Than Their Manager

EasySQL#self-join~15m

Problem

A table employee(id, name, salary, manager_id) holds everyone in a company, and manager_id is the id of the person they report to, or NULL at the top. Write a query that returns every employee who earns strictly more than their direct manager, as columns name, salary and manager_salary, sorted by name.

Examples

Input:  employee = [(1, "Ana", 120, None), (2, "Ben", 90, 1), (3, "Cleo", 130, 1), (4, "Dev", 95, 3), (5, "Finn", 100, 4)]
Output: [("Cleo", 130, 120), ("Finn", 100, 95)]
Why:    Cleo out-earns Ana, and Finn out-earns Dev, who reports to Cleo
Input:  employee = [(1, "Ana", 100, None), (2, "Ben", 100, 1)]
Output: []
Why:    edge case, an equal salary is not more

Hints

0 / 3

Stuck on the idea rather than the code? INNER and OUTER JOINs covers it.