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
Employees Earning More Than Managers INPUT: Employee Table ID Name Salary MgrID 1 Joe 70000 3 2 Henry 80000 4 3 Sam 60000 - 4 Max 90000 - Hierarchy View Sam $60K Max $90K Joe $70K Henry $80K Mgr ID 3 Mgr ID 4 70K > 60K 80K < 90K ALGORITHM: Self Join 1 Self Join Table Join Employee as e1, e2 e1.managerId = e2.id 2 Link to Manager e1=employee, e2=manager 3 Compare Salaries e1.salary > e2.salary 4 Return Names SELECT e1.name SELECT e1.name FROM Employee e1 JOIN Employee e2 ON e1.managerId=e2.id WHERE e1.salary>e2.salary e1.mgrId matches e2.id Compare: e1.sal > e2.sal FINAL RESULT Employee Name Joe Joe earns $70,000 His manager Sam earns $60,000 70K > 60K = OK Henry NOT included: Henry: $80,000 Max (mgr): $90,000 Output: ["Joe"] Key Insight: Self-Join Pattern A self-join allows comparing rows within the same table. By joining Employee to itself, we can match each employee with their manager and compare their salaries directly. Time: O(n) | Space: O(n) where n = number of employees TutorialsPoint - Employees Earning More Than Their Managers | Optimal Solution: Self-Join
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