181. Employees Earning More Than Their Managers - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

We are given a table called Employee with the following columns:

  • id: The unique identifier for each employee.

  • name: The employee's name.

  • salary: The employee's salary.

  • managerId: The id of the employee’s manager (this value is NULL if the employee does not have a manager).

The task is to write a SQL query that returns the names of all employees who earn more than their respective managers.

Example

Consider the following sample data in the Employee table:

idnamesalarymanagerId
1Joe700003
2Henry800004
3Sam60000NULL
4Max900003

Explanation:

  • Joe (id 1):
    • Manager: Sam (id 3) with a salary of 60000.
    • Since 70000 > 60000, Joe qualifies.
  • Henry (id 2):
    • Manager: Max (id 4) with a salary of 90000.
    • Since 80000 < 90000, Henry does not qualify.
  • Max (id 4):
    • Manager: Sam (id 3) with a salary of 60000.
    • Since 90000 > 60000, Max qualifies.

Expected Output:
The query should return the names "Joe" and "Max".

Constraints

  • Every employee except the top-level manager has a valid managerId.
  • The table can have multiple employees reporting to the same manager.
  • Some employees might not have a manager (their managerId is NULL), and they should not be considered in the comparison.

Hints

  1. Self-Join:
    • Think about how you can compare rows within the same table. In this case, you need to compare an employee’s salary with his/her manager’s salary. This is a perfect scenario for a self-join.
  2. Filtering with Conditions:
    • Use a join condition to connect each employee with his/her manager and then apply a WHERE clause to filter out only those cases where the employee’s salary is greater than the manager’s salary.

Approach: Using a Self-Join

Explanation

  1. Self-Join Concept:
    • Since both employees and managers reside in the same Employee table, you can perform a self-join.

    • Create two aliases for the table, for example, e (for employee) and m (for manager).

  2. Join Condition:
    • Link the two aliases by matching e.managerId with m.id. This effectively pairs each employee with their manager.
  3. Filtering Condition:
    • Use a WHERE clause to select only those pairs where the employee’s salary (e.salary) is greater than the manager’s salary (m.salary).
  4. Result:
    • Select the employee’s name from the resulting set.

SQL Query (Solution)

SELECT e.name AS Employee FROM Employee e JOIN Employee m ON e.managerId = m.id WHERE e.salary > m.salary;

Walkthrough

  1. Alias Assignment:
    • We create two aliases: e for the employees and m for the managers.
  2. Join Operation:
    • We join the table on the condition that e.managerId = m.id. This pairs each employee with their corresponding manager.
  3. Applying the Filter:
    • The WHERE clause ensures that only those records are selected where the employee's salary exceeds that of the manager.
  4. Result Column:
    • We select e.name and alias it as Employee for clarity in the result.

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Common Mistakes

  • Forgetting the Self-Join:
    • Some might try to compare salaries within a single instance of the table without joining, which does not work because the manager’s details are in different rows.
  • Not Handling NULL Manager IDs:
    • Ensure that you only consider rows where managerId is not NULL, as these rows represent employees with an actual manager. The join condition naturally filters out employees without a manager since NULL will not match any id.
  • Using Incorrect Join Type:
    • Using a LEFT JOIN instead of an INNER JOIN could inadvertently include employees without a valid manager. An INNER JOIN is preferred because it only returns rows with matching manager records.

Edge Cases

  • Employees without Managers:

    • Employees who do not have a manager (i.e., their managerId is NULL) will not appear in the join and thus are correctly excluded from the results.
  • Equal Salaries:

    • If an employee and their manager have equal salaries, the employee should not be included since the problem specifies strictly greater salaries.

Alternative Variations

  • Reporting Additional Information:

    • The query can be extended to also return the manager’s name along with the employee’s name and salary details.
  • Sorting the Results:

    • You might also be asked to sort the result based on the employee’s name or salary. Simply add an ORDER BY clause to the query if needed.
  • Handling Ties or Multiple Conditions:

    • Variations might include additional filters, such as comparing departments or other attributes, which would require modifying the join or filtering conditions accordingly.
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;