데이터 분석

181. Employees Earning More Than Their Managers | Easy📌 본문

SQL/leetcode

181. Employees Earning More Than Their Managers | Easy📌

딱한아이 2025. 1. 10. 18:15
문제

 

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

Return the result table in any order.

테이블 정보

 

예시

 

본인의 매니저보다 급여가 높은 직원의 이름을 조회하는 문제이다.

Joe의 salary 옆에 Sam의 salary를 붙여놓고 비교하는 방식으로 접근하면 해결할 수 있을 것이다.

 

Solution

 

✅ 동일한 두 테이블을 SELF-JOIN하여 접근한다.

 

● e1 테이블은 기존에 기록된 직원들의 정보를 알기 위해 사용

● e2 테이블은 기존에 기록된 직원들의 매니저 정보를 알기 위해 사용

SELECT e.name AS Employee
FROM Employee e
    JOIN Employee m ON e.managerID = m.id
WHERE e.salary > m.salary
    -- AND e.managerID IS NOT NULL

 

INNER JOIN을 할 경우, managerID가 Null인 Sam/Max는 조인이 되지 않기 때문에 e1.managerID IS NOT NULL 조건이 필요가 없다.

INNER JOIN 결과