본문 바로가기
취준/코팅 테스트

[LeetCode] 1978. Employees Whose Manager Left the Company

by 소복소복뚜벅뚜벅 2024. 4. 1.

문제 

Find the IDs of the employees whose salary is strictly less than $30000 and whose manager left the company. When a manager leaves the company, their information is deleted from the Employees table, but the reports still have their manager_id set to the manager that left.

Return the result table ordered by employee_id.

 

 

문제 풀이 

SELECT employee_id 
FROM Employees 
WHERE salary < 30000 
AND manager_id NOT IN (
	SELECT employee_id 
    FROM Employees 
) 
ORDER BY employee_id

 

1. WHERE 절에 조건 중에 하나인 Salary 가 30000달라 미만인 조건을 작성한다. 

2. 그 다음, manager _id가 없는 직원이어야 하기 때문에 employee_id의 번호가 포함되지 않아야 한다. 이때 서브쿼리를 이용해서 작성한다. 

3. 마지막에 employee_id를 정렬한다. 

 

오답이유 

self join으로 어렵게 풀려고 했지만, NULL로 인해 사용이 어려웠음. 자꾸 서브 쿼리를 쓰는 방법을 생각해 내지 못하는 것이 문제 인듯 함.