Employees Earning More Than Their Manager
Find employees whose salary is greater than their direct manager's salary.
Start practiceProblem 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), andmanager_salary. - The employee with no manager (the CEO) can never qualify.
- An employee whose
manager_idpoints to a missing row cannot qualify either. - "Greater than" is strictly greater — equal salaries do not qualify.
Example
| id | name | salary | manager_id |
|---|---|---|---|
| 1 | Alice | 100 | NULL |
| 2 | Bob | 80 | 1 |
| 3 | Carol | 120 | 1 |
| 4 | Dave | 90 | 2 |
Expected result:
| employee | employee_salary | manager | manager_salary |
|---|---|---|---|
| Carol | 120 | Alice | 100 |
| Dave | 90 | Bob | 80 |
Constraints
employee.idis the primary key.employee.manager_idis nullable and referencesemployee.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
- easyAverage Process Time Per MachineCompute each machine's average time to complete a process.
- easyCustomers Without OrdersList customers who have never placed an order using an anti-join.
- easyEmployee BonusReport every employee's name and bonus, including those with none.
- easyVisits Without TransactionsCount visits with no transactions per customer.
- mediumDepartments by Average SalaryFind departments whose average salary is above the company average.