Employees Earning More Than Their Managers - Problem

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

Employee
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
Primary Key: id
Note: Each row represents one employee. The managerId can be NULL for top-level executives who have no manager.

Input & Output

Example 1 — Basic Employee-Manager Comparison
Input Table:
id name salary managerId
1 Joe 70000 3
2 Henry 80000 4
3 Sam 60000
4 Max 90000
Output:
name
Joe
💡 Note:

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.

Example 2 — Multiple High Earners
Input Table:
id name salary managerId
1 Alice 85000 3
2 Bob 75000 3
3 Charlie 70000
4 Diana 95000 5
5 Eve 80000
Output:
name
Alice
Bob
Diana
💡 Note:

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.

Example 3 — No High Earners
Input Table:
id name salary managerId
1 John 50000 2
2 Manager 80000
Output:
name
💡 Note:

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
  • managerId is a foreign key to id or NULL
  • All employee names are unique

Visualization

Tap to expand
Finding Employees Who Earn More Than Their ManagersEmployee TableidnamesalarymgrId1Joe7000032Henry8000043Sam60000NULL4Max90000NULLSELF JOINe.managerId = m.idWHERE e.salary > m.salaryJoin Resultemp_nameemp_salmgr_namemgr_salqualifyJoe70000Sam60000Henry80000Max90000Final ResultnameJoeOnly Joe ($70K) earns more than his manager Sam ($60K)
Understanding the Visualization
1
Input Table
Employee table with salary and manager relationships
2
Self Join
Join table with itself on managerId = id
3
Filter & Select
Where employee salary > manager salary
Key Takeaway
🎯 Key Insight: Self-joins are perfect for comparing records within the same table using foreign key relationships
Asked in
Amazon 12 Google 8 Microsoft 15 Meta 6
187.4K Views
High Frequency
~8 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen