Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet code] 1978. Employees Whose Manager Left the Company

์ธ” 2023. 10. 11. 16:51

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ 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;

 

'Growth ๐ŸŒณ > Practice ๐Ÿ’ป' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leet Code] 1280. Students and Examinations  (0) 2023.10.17
[Leet Code] 1965. Employees With Missing Information  (0) 2023.10.12
[Leet code] 1084. Sales Analysis III  (0) 2023.10.10
[Leet Code] 1068. Product Sales Analysis I  (1) 2023.10.07
[Leet code] 1075. Project Employees I  (1) 2023.10.06