Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet Code] 1965. Employees With Missing Information

์ธ” 2023. 10. 12. 22:55

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

       ๋ฆฌํŠธ์ฝ”๋“œ Problems, https://leetcode.com/problemset/all/์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.


โœ” ๋ฌธ์ œ

https://leetcode.com/problems/employees-with-missing-information/description/

 

Employees With Missing Information - LeetCode

Can you solve this real interview question? Employees With Missing Information - Table: Employees +-------------+---------+ | Column Name | Type | +-------------+---------+ | employee_id | int | | name | varchar | +-------------+---------+ employee_id is t

leetcode.com

 

Table : Employees

Table : Salaries

Write a solution to report the IDs of all the employees with missing information. The information of an employee is missing if:

  • The employee's name is missing, or
  • The employee's salary is missing.

Return the result table ordered by employee_id in ascending order.


โœ” ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

์ •๋ณด๊ฐ€ ๋ˆ„๋ฝ๋œ ๋ชจ๋“  ์ง์›์˜ ID๋ฅผ ID ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

์ •๋ณด๊ฐ€ ๋ˆ„๋ฝ๋œ ๊ฒฝ์šฐ๋Š” ์ง์› ์ด๋ฆ„์ด ๋ˆ„๋ฝ๋˜๊ฑฐ๋‚˜, ๊ธ‰์—ฌ๊ฐ€ ๋ˆ„๋ฝ๋œ ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค.

MySQL์— FULL OUTER JOIN ๊ฐœ๋…์ธ UNION์„ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ ํ…Œ์ด๋ธ”์˜ employee_id ๊ธฐ์ค€์œผ๋กœ ์กฐ์ธํ•ด์ฃผ๊ณ 

missing๋œ ๋ฐ์ดํ„ฐ(null๊ฐ’)์˜ employee_id๋ฅผ UNION ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค.

 

๋‚˜๋Š” UNION ์•„๋ž˜ ๊ฒฐ๊ณผ๋ฅผ Salaries์™€ Employees ์œ„์น˜๋ฅผ ๋ฐ”๊ฟ”์ฃผ๊ณ  left join ์ฒ˜๋ฆฌ ํ–ˆ์ง€๋งŒ ์œ„์— ์ฟผ๋ฆฌ์ฒ˜๋Ÿผ Employees๋ฅผ FROM์ ˆ์— ๋‘๊ณ  right join ํ•ด๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

SELECT T1.employee_id AS employee_id
 FROM Employees T1
 LEFT JOIN Salaries T2
 ON T1.employee_id = T2.employee_id
 WHERE salary is null
  
 UNION
 
 SELECT T2.employee_id AS employee_id
 FROM Salaries T2
 LEFT JOIN Employees T1
 ON T1.employee_id = T2.employee_id
 WHERE name is null
 
 ORDER BY 1;

 

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

[Leet Code] 1251. Average Selling Price  (0) 2023.10.18
[Leet Code] 1280. Students and Examinations  (0) 2023.10.17
[Leet code] 1978. Employees Whose Manager Left the Company  (0) 2023.10.11
[Leet code] 1084. Sales Analysis III  (0) 2023.10.10
[Leet Code] 1068. Product Sales Analysis I  (1) 2023.10.07