[Leet code] 1978. Employees Whose Manager Left the Company
๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/sales-analysis-iii/
LeetCode - The World's Leading Online Programming Learning Platform
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
Table : Employees
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.
…
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
๊ธ์ฌ๊ฐ $30000 ๋ฏธ๋ง์ด๊ณ ๊ด๋ฆฌ์๊ฐ ํ์ฌ๋ฅผ ๋ ๋ ์ง์์ ID๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ถ๋ ฅํด์ผ ํ๋ค.
๋ฌธ์ ๋ฅผ ํ ๋ ์ฃผ์ํด์ผ ํ ์ ์
1. ๊ด๋ฆฌ์๊ฐ ์๋ ์ง์์ manager_id ๊ฐ์ด null์ด๋ค.
2. ๊ด๋ฆฌ์๊ฐ ํ์ฌ๋ฅผ ๋ ๋๋ฉด ์ง์ ํ ์ด๋ธ์์ ํด๋น ์ ๋ณด๊ฐ ์ญ์ ๋์ง๋ง ๋ณด๊ณ ์์๋ ์ฌ์ ํ ๊ด๋ฆฌ์ ID๊ฐ ๋ ๋ ๊ด๋ฆฌ์๋ก ์ค์ ๋์ด ์๋ค.
์ฃผ์์ ์ ๊ณ ๋ คํ์ฌ where์ ์กฐ๊ฑด์ ์ธ๊ฐ์ง ๋ถ์ฌํ๊ณ ์๋์ ๊ฐ์ด ์ถ๋ ฅํ๋ค.
์กฐ๊ฑด1) manager_id ๊ฐ์ด null์ด ์๋ ๊ฒ ('๊ด๋ฆฌ์๊ฐ ํ์ฌ๋ฅผ ๋ ๋'์กฐ๊ฑด์ '๊ด๋ฆฌ์๊ฐ ์์๋ค'๋ก ๋ถํฉ์ํด)
์กฐ๊ฑด2) ์ฐ๋ด์ด $30000 ๋ฏธ๋ง์ผ ๊ฒ.
์กฐ๊ฑด1-1) manager_id๊ฐ employee_id ์ ์์ ๊ฒ ('๊ด๋ฆฌ์๊ฐ ํ์ฌ๋ฅผ ๋ ๋' ์กฐ๊ฑด์ '๊ด๋ฆฌ์๊ฐ ํ์ฌ ํ์ฌ์ ์ฌ์ง์ค์ด ์๋๋ค'๋ฅผ ๋ถํฉ)
SELECT employee_id
FROM Employees
WHERE manager_id is not null
AND salary < 30000
AND manager_id NOT IN (select employee_id FROM Employees)
ORDER BY 1;