Skip to main content

181. Employees Earning More Than Their Managers

Giới thiệu bài toán

Chi tiết: https://leetcode.com/problems/employees-earning-more-than-their-managers/

Table: Employee

Column NameType
idint
namevarchar
salaryint
managerIdint
  • id is the primary key (column with unique values) for this table.
  • Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager.
Yêu cầu

Write a solution to find the employees who earn more than their managers.

Return the result table in any order. The result format is in the following example.

Example 1:

  • Input: Employee table:
idnamesalarymanagerId
1Joe700003
2Henry800004
3Sam60000Null
4Max90000Null
  • Output:
Employee
Joe
  • Explanation: Joe is the only employee who earns more than his manager.

Giải quyết bài toán

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

Tham khảo: https://leetcode.com/submissions/detail/1039195164/