Managers Earning Less Than Their Direct Subordinates
Quick Overview
Write a SQL query that pairs each manager with every direct report who earns more than the manager, returning both names and salaries in a fixed order. It tests self-joins, handling of employees with no manager, strict comparisons and deterministic tie-breaking.
Managers Earning Less Than Their Direct Subordinates
Company: EPAM Systems
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Given an `employees` table where each row can point to the employee's direct manager, find every (manager, direct subordinate) pair in which the manager earns strictly less than the subordinate. For each pair, return the manager's name and salary and the subordinate's name and salary.
```hint Where the two salaries live
The manager's salary and the subordinate's salary sit on different rows of the same table. Work out how to bring both onto one result row before you compare them.
```
### Input Table(s)
Table `employees`
| Column | Type | Meaning |
|---|---|---|
| `id` | INT | Employee id. Primary key. |
| `name` | VARCHAR(100) | Employee name. NOT NULL. |
| `salary` | DECIMAL(10,2) | Employee salary. NOT NULL, non-negative. |
| `manager_id` | INT | `id` of the employee's direct manager. NULL means the employee has no manager. |
Keys: `id` is the primary key. `manager_id` is a nullable foreign key that references `employees(id)`.
### Output Contract
Return one row per qualifying (manager, direct subordinate) pair, with exactly these columns in this order:
| Column | Meaning |
|---|---|
| `Manager` | The manager's `name` |
| `ManagerSalary` | The manager's `salary` |
| `Employee` | The subordinate's `name` |
| `EmployeeSalary` | The subordinate's `salary` |
Return the salaries as stored, with no rounding. Column names must match the casing shown. Order the rows by `ManagerSalary` ascending, then manager `id` ascending, then `EmployeeSalary` descending, then employee `id` ascending. Each employee has at most one manager, so the employee `id` is unique across the result and the order is fully determined.
### Example
Input `employees`:
| id | name | salary | manager_id |
|---|---|---|---|
| 1 | Alice | 90000 | NULL |
| 2 | Bob | 80000 | 1 |
| 3 | Charlie | 95000 | 1 |
| 4 | Diana | 85000 | 2 |
| 5 | Eve | 70000 | 2 |
Expected output:
| Manager | ManagerSalary | Employee | EmployeeSalary |
|---|---|---|---|
| Bob | 80000 | Diana | 85000 |
| Alice | 90000 | Charlie | 95000 |
Alice (90000) manages Bob and Charlie. Charlie earns 95000, more than Alice, so (Alice, Charlie) is returned. Bob earns 80000, less than Alice, so (Alice, Bob) is not. Bob (80000) manages Diana (85000) and Eve (70000), and only Diana earns more than Bob, so (Bob, Diana) is returned. Bob's pair comes first because his salary is lower than Alice's.
### Constraints and Clarifications
- `employees` has at most 10,000 rows.
- `manager_id` is either NULL or a valid `id`, and there are no circular references, so no employee is their own manager.
- `salary` is never NULL and never negative.
- The comparison is strict. A subordinate who earns exactly the manager's salary is not returned.
- Only direct reports count. A manager is compared with the employees whose `manager_id` is that manager's `id`, never with skip-level reports.
- A manager with several higher-paid direct reports appears once for each of them.
- An employee with a NULL `manager_id` can appear only in the manager columns.
- Names are not guaranteed to be unique, so use `id` to tell people apart.
- If no pair qualifies, return an empty result with the four columns.
Overview: Write a SQL query that pairs each manager with every direct report who earns more than the manager, returning both names and salaries in a fixed order. It tests self-joins, handling of employees with no manager, strict comparisons and deterministic tie-breaking.
Given an employees table where each row can point to the employee's direct manager, find every (manager, direct subordinate) pair in which the manager earns strictly less than the subordinate. For each pair, return the manager's name and salary and the subordinate's name and salary.
Input Table(s)
Table employees
Column
Type
Meaning
id
INT
Employee id. Primary key.
name
VARCHAR(100)
Employee name. NOT NULL.
salary
DECIMAL(10,2)
Employee salary. NOT NULL, non-negative.
manager_id
INT
id of the employee's direct manager. NULL means the employee has no manager.
Keys: id is the primary key. manager_id is a nullable foreign key that references employees(id).
Output Contract
Return one row per qualifying (manager, direct subordinate) pair, with exactly these columns in this order:
Column
Meaning
Manager
The manager's name
ManagerSalary
The manager's salary
Employee
The subordinate's name
EmployeeSalary
The subordinate's salary
Return the salaries as stored, with no rounding. Column names must match the casing shown. Order the rows by ManagerSalary ascending, then manager id ascending, then EmployeeSalary descending, then employee id ascending. Each employee has at most one manager, so the employee id is unique across the result and the order is fully determined.
Example
Input employees:
id
name
salary
manager_id
1
Alice
90000
NULL
2
Bob
80000
1
3
Charlie
95000
1
4
Diana
85000
2
5
Eve
70000
2
Expected output:
Manager
ManagerSalary
Employee
EmployeeSalary
Bob
80000
Diana
85000
Alice
90000
Charlie
95000
Alice (90000) manages Bob and Charlie. Charlie earns 95000, more than Alice, so (Alice, Charlie) is returned. Bob earns 80000, less than Alice, so (Alice, Bob) is not. Bob (80000) manages Diana (85000) and Eve (70000), and only Diana earns more than Bob, so (Bob, Diana) is returned. Bob's pair comes first because his salary is lower than Alice's.
Constraints and Clarifications
employees
has at most 10,000 rows.
manager_id
is either NULL or a valid
id
, and there are no circular references, so no employee is their own manager.
salary
is never NULL and never negative.
The comparison is strict. A subordinate who earns exactly the manager's salary is not returned.
Only direct reports count. A manager is compared with the employees whose
manager_id
is that manager's
id
, never with skip-level reports.
A manager with several higher-paid direct reports appears once for each of them.
An employee with a NULL
manager_id
can appear only in the manager columns.
Names are not guaranteed to be unique, so use
id
to tell people apart.
If no pair qualifies, return an empty result with the four columns.