Given an Employee table, write a SQL query to find all employees who earn more than their direct managers.
The Employee table contains employee information including their ID, name, salary, and manager ID. Each employee has a unique ID, and the managerId column references the ID of their direct manager.
Task: Return the names of employees whose salary is greater than their manager's salary.
Table Schema
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key, unique employee ID |
name
|
varchar | Employee's full name |
salary
|
int | Employee's salary amount |
managerId
|
int | Foreign key referencing the manager's employee ID |
Input & Output
| id | name | salary | managerId |
|---|---|---|---|
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | |
| 4 | Max | 90000 |
| name |
|---|
| Joe |
Joe earns $70,000 and his manager Sam earns $60,000, so Joe earns more than his manager. Henry earns $80,000 but his manager Max earns $90,000, so Henry does not earn more than his manager. Sam and Max have no managers (managerId is NULL), so they are not included in the comparison.
| id | name | salary | managerId |
|---|---|---|---|
| 1 | Alice | 85000 | 3 |
| 2 | Bob | 75000 | 3 |
| 3 | Charlie | 70000 | |
| 4 | Diana | 95000 | 5 |
| 5 | Eve | 80000 |
| name |
|---|
| Alice |
| Bob |
| Diana |
Alice ($85K) and Bob ($75K) both earn more than their manager Charlie ($70K). Diana ($95K) earns more than her manager Eve ($80K). Charlie and Eve have no managers, so they're not compared against anyone.
| id | name | salary | managerId |
|---|---|---|---|
| 1 | John | 50000 | 2 |
| 2 | Manager | 80000 |
| name |
|---|
John earns $50,000 while his manager earns $80,000. Since John does not earn more than his manager, no employees are returned in the result.
Constraints
-
1 ≤ Employee.id ≤ 500 -
0 ≤ Employee.salary ≤ 100000 -
managerIdis a foreign key toidorNULL - All employee names are unique