easyJOINsSelf JoinNULL handling
Employees Earning More Than Their Manager
Find employees whose salary is greater than their direct manager's salary.
Start practicePrompt
Employees Earning More Than Their Manager
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.
Expected concepts
- Self join
- Aliasing
- Comparison with NULL
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.