Post

[LeetCode 181] Employees Earning More Than Their Managers

LeetCode 181 (SQL)
[Employees Earning More Than Their Managers] 문제 풀이

[LeetCode 181] Employees Earning More Than Their Managers

문제 바로가기


Description


Table: Employee

1
2
3
4
5
6
7
8
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
| salary      | int     |
| managerId   | int     |
+-------------+---------+

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.

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:
      1
      2
      3
      4
      5
      6
      7
      8
      
      +----+-------+--------+-----------+
      | id | name  | salary | managerId |
      +----+-------+--------+-----------+
      | 1  | Joe   | 70000  | 3         |
      | 2  | Henry | 80000  | 4         |
      | 3  | Sam   | 60000  | Null      |
      | 4  | Max   | 90000  | Null      |
      +----+-------+--------+-----------+
      
  • Output:
    1
    2
    3
    4
    5
    
    +----------+
    | Employee |
    +----------+
    | Joe      |
    +----------+
    
  • Explanation: Joe is the only employee who earns more than his manager.


Constraints








Code


내 제출


1
2
3
4
5
6
7
# Write your MySQL query statement below

SELECT e1.name Employee
FROM
    Employee e1
    JOIN Employee e2 ON e1.managerId = e2.id
WHERE e1.salary > e2.salary;


RuntimeMemory
324 ms0.0 B


다른 풀이


1
2
3
4
5
# Write your MySQL query statement below

SELECT EMP.name AS Employee
FROM Employee EMP,Employee MGR
WHERE EMP.managerId=MGR.id AND EMP.salary>MGR.salary


Reference


  • https://github.com/doocs/leetcode/tree/main/solution/0100-0199/0181.Employees%20Earning%20More%20Than%20Their%20Managers
This post is licensed under CC BY 4.0 by the author.