문제
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로 인해 사용이 어려웠음. 자꾸 서브 쿼리를 쓰는 방법을 생각해 내지 못하는 것이 문제 인듯 함.
'취준 > 코팅 테스트' 카테고리의 다른 글
[LeetCode] - 1661. Average Time Of Process per Machine (0) | 2024.04.04 |
---|---|
[LeetCode] - 1321. Restaurant Growth (0) | 2024.04.03 |
[LeetCode] - 1890. The Latest Login in 2020 (0) | 2024.03.29 |
[LeetCode] - 1251. Average Selling Price (1) | 2024.03.28 |
[LeetCode] - 180. Consecutive Numbers (0) | 2024.03.27 |