Sweaty Imposter

Employees Who Out-Earn Their Manager

SQLSelf-joinMidSeniorUpdated
Return the names of employees who earn strictly more than their direct manager. Employees with no manager should not appear.

Schema

CREATE TABLE employees (
  id         BIGINT PRIMARY KEY,
  name       TEXT NOT NULL,
  manager_id BIGINT REFERENCES employees(id),
  salary     NUMERIC NOT NULL
);

Drill This: Your Query First

The Correct Query

Option (c)

SELECT e.name
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.salary > m.salary;
An INNER self-join on manager_id pairs each employee with their direct manager and excludes top-level employees (manager_id IS NULL) automatically. (a) is a cross join and compares everyone to everyone. (b) re-adds top-level employees via the OR clause, violating the spec. (d) compares to the company average, not to the manager.

The Options That Fail

The explanation above says why each of these breaks.

Option (a)

SELECT e.name
FROM employees e, employees m
WHERE e.salary > m.salary;

Option (b)

SELECT e.name
FROM employees e
LEFT JOIN employees m ON m.id = e.manager_id
WHERE e.salary > m.salary OR m.id IS NULL;

Option (d)

SELECT e.name
FROM employees e
WHERE e.salary > (SELECT AVG(salary) FROM employees);