Leetcode Problem 181. Employees Earning More Than Their Managers

181. Employees Earning More Than Their Managers

Leetcode Solutions

Using JOIN to Find Employees Earning More Than Their Managers

Algorithm

  1. Perform a JOIN operation on the Employee table with itself, aliasing one instance as a (representing the employee) and the other as b (representing the manager).
  2. Use the ON clause to specify the join condition: a.managerId = b.id. This condition ensures that we are comparing each employee with their respective manager.
  3. Add an additional condition to the ON clause: a.salary > b.salary. This filters the joined rows to only include those where the employee's salary is greater than the manager's salary.
  4. Select the name of the employee from the joined table and alias it as Employee for the output.
  5. The resulting query will return the names of employees who earn more than their managers.

erDiagram
    Employee {
        int id PK
        varchar name
        int salary
        int managerId FK
    }

Using WHERE Clause for Self-Join Comparison

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...