Skip to content
>_sqlbuddy

Employees Earning More Than Their Manager

Find employees whose salary is greater than their direct manager's salary.

Start practice

Problem statement

Prompt

Given the employee table below, write a query that returns every employee whose salary is greater than their direct manager's salary.

  • The result should include: employee (the employee's name), employee_salary, manager (the manager's name), and manager_salary.
  • The employee with no manager (the CEO) can never qualify.
  • An employee whose manager_id points to a missing row cannot qualify either.
  • "Greater than" is strictly greater — equal salaries do not qualify.

Example

idnamesalarymanager_id
1Alice100NULL
2Bob801
3Carol1201
4Dave902

Expected result:

employeeemployee_salarymanagermanager_salary
Carol120Alice100
Dave90Bob80

Constraints

  • employee.id is the primary key.
  • employee.manager_id is nullable and references employee.id.

Tips

  • An employee's manager is another row in the same table — use a self join with table aliases.
  • Joining on the unique primary key means no fan-out: one row per employee.

Schema

CREATE TABLE employee (
    id         INTEGER PRIMARY KEY,
    name       TEXT NOT NULL,
    salary     INTEGER NOT NULL,
    manager_id INTEGER REFERENCES employee(id)  -- NULL for top-level employees
);

Sample data

INSERT INTO employee (id, name, salary, manager_id) VALUES
  (1, 'Alice', 100, NULL),
  (2, 'Bob',   80,  1),
  (3, 'Carol', 120, 1),
  (4, 'Dave',  90,  2),
  (5, 'Eve',   70,  2);

Additional hidden fixtures are applied during validation to test edge cases.

Solution

One correct approach — try solving it yourself in the practice editor first, then compare. There is usually more than one valid solution.

-- Reference: employees strictly earning more than their direct manager.
SELECT e.name AS employee,
       e.salary AS employee_salary,
       m.name AS manager,
       m.salary AS manager_salary
FROM employee e
JOIN employee m ON m.id = e.manager_id
WHERE e.salary > m.salary
ORDER BY e.id;

Key concepts

  • Self join
  • Aliasing
  • Comparison with NULL

Related questions

Learn more