02/28 SQL 문제풀이 - 1978. Employees Whose Manager Left the Company (Leetcode)

Data Architect / Engineer·2024년 2월 28일
1

1일_1SQL

목록 보기
41/63
post-thumbnail

문제

  • LeetCode SQL 문제
  • 1978. Employees Whose Manager Left the Company / Easy
  • 문제 내용 : [링크]


내가 작성한 Query

select e.employee_id
from employees e
    left join employees m on e.manager_id = m.employee_id
where e.salary < 30000 and m.employee_id is null and e.manager_id is not null
order by e.employee_id
  • 직원의 정보를 employees e 테이블, 매니저의 정보를 employees m 테이블로 지정하고 SELF JOIN 해 준다.

  • 이 때, 직원의 manager_id와 매니저의 employee_id가 같은 조건을 이용해 JOIN 해 주되, 퇴사한 매니저의 경우 employees m 테이블에 employee_id 값이 없으므로 LEFT JOIN 해 주어야 한다. 그냥 JOIN을 해 주면, 퇴사한 매니저를 가진 직원의 정보를 출력할 수 없다.

  • WHERE 절에 salary가 30000보다 작고, 매니저의 employee_idnull이며, 직원의 매니저 아이디는 null 값이 아닌 데이터 조건을 걸어준다.

  • e.employee_id 값을 출력해주고 ORDER BY를 활용하여 정렬해준다.

  • ⭐⭐ 중요
  1. SELF JOIN 시, 조건을 통해 직원-매니저, 고객-추천인 등의 정보를 출력할 수 있다.
  2. 데이터 출력 레벨에 따라 출력해야 하는 데이터 레벨에 맞게 LEFT JOIN 또는 JOIN 해 준다.

profile
질문은 계속돼 아오에

0개의 댓글