# Higher Than Supervisor

> When the student outscores the teacher.

Canonical URL: <https://datadriven.io/problems/higher_than_supervisor>

Domain: SQL · Difficulty: easy · Seniority: L4

## Problem

Surface employees whose salary is higher than their direct manager's. Pull the employee's name and salary alongside their manager's name and salary, excluding anyone who reports to no one.

## Worked solution and explanation

### Why this problem exists in real interviews

Finding employees who earn more than their manager tests **self-join** mechanics. The interviewer checks whether you can join a table to itself on the manager relationship and apply a salary comparison.

> **Trick to Solving**
>
> A self-join aliases the same table twice. Join `employees e` to `employees m` on `e.manager_id = m.employee_id`, then filter `WHERE e.salary > m.salary`.
> 
> 1. Alias the table as employee and manager
> 2. Join on the manager_id relationship
> 3. Compare salaries across aliases

---

### Break down the requirements

#### Step 1: Self-join on manager relationship

`JOIN employees m ON e.manager_id = m.employee_id` pairs each employee with their manager.

#### Step 2: Filter where employee earns more

`WHERE e.salary > m.salary` isolates employees who out-earn their supervisor.

---

### The solution

**Self-join with salary comparison**

```sql
SELECT e.employee_id, e.emp_name AS employee_name, e.salary AS employee_salary,
       m.emp_name AS manager_name, m.salary AS manager_salary
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id
WHERE e.salary > m.salary
ORDER BY e.salary - m.salary DESC
```

> **Cost Analysis**
>
> The self-join on `manager_id = employee_id` uses the primary key index. Lightweight for typical employee tables.

> **Interviewers Watch For**
>
> The interviewer checks that you get the join direction right: the employee's `manager_id` matches the manager's `employee_id`, not the reverse.

> **Common Pitfall**
>
> Joining on `e.employee_id = m.manager_id` reverses the relationship and gives subordinates, not the manager.

---

## Common follow-up questions

- What about employees with no manager (CEO)? _(Tests that INNER JOIN naturally excludes them.)_
- How would you show the salary difference as a percentage? _(Tests (e.salary - m.salary) / m.salary * 100.)_
- How would you find employees earning more than their skip-level manager? _(Tests chaining another self-join.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/higher_than_supervisor)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.