Employees Earning More Than Their Managers 문제 바로가기
Employees Earning More Than Their Managers - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제
Write an SQL query to find the employees who earn more than their managers.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input:
Employee table:
+----+-------+--------+-----------+
| id | name | salary | managerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | Null |
| 4 | Max | 90000 | Null |
+----+-------+--------+-----------+
Output:
+----------+
| Employee |
+----------+
| Joe |
+----------+
Explanation: Joe is the only employee who earns more than his manager.
풀이
-- employee 테이블에 managerID를 기준으로 SELF JOIN -> manager 테이블을 새로 만들기
-- manager보다 salary가 employee 조건문 WHERE로 필터링
SELECT em.name AS Employee
FROM employee AS em
INNER JOIN employee AS ma ON em.managerID = ma.id
WHERE em.salary > ma.salary
OUTPUT
{"headers": ["Employee"], "values": [["Joe"]]}
'SQL > MySQL 문제풀이' 카테고리의 다른 글
[HackerRank] Symmetric Pairs Solution(풀이) (0) | 2022.05.29 |
---|---|
[Leetcode] Rising Temperature Solution(풀이) (0) | 2022.05.24 |
[SQL LEFT JOIN][Leetcode] Customer Who Never Order (풀이) (0) | 2022.05.23 |
[SQL INNER JOIN][HackerRank] Average Population of Each Continent(해석/풀이) (0) | 2022.05.23 |
[SQL INNER JOIN][HackerRank] Population Census (풀이/해석) (0) | 2022.05.23 |