๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL/LeetCode

[LeetCode][SQL50] #6. Replace Employee ID With The Unique Identifier

by ์ด๋ฎด 2023. 11. 7.
728x90

๐Ÿ’ป ๋ฌธ์ œ ์ฃผ์†Œ : Replace Employee ID With The Unique Identifier - LeetCode

 

Replace Employee ID With The Unique Identifier - LeetCode

Can you solve this real interview question? Replace Employee ID With The Unique Identifier - Table: Employees +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+ id is t

leetcode.com

 

๐Ÿ’ป ๋ฌธ์ œ

Write a solution to show the unique ID of each user. If a user does not have a unique ID replace just show null. Return the result table in any order.

- ๊ฐ ์‚ฌ์šฉ์ž์˜ ๊ณ ์œ  ID๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค. ๋งŒ์•ฝ ๊ณ ์œ  ID๊ฐ€ ์—†์„ ๊ฒฝ์šฐ, null์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜์‹œ์˜ค. ์ถœ๋ ฅ ์ˆœ์„œ๋Š” ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค.

Input๊ณผ Output ์˜ˆ์‹œ

 

๐Ÿ’ป ์ฝ”๋“œ ๋ฐ ํ’€์ด

select  uni.unique_id, e.name
from Employees e
left join EmployeeUNI uni on e.id = uni.id

 

์œ„ ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ Employees, EmployeeUNI, ์ด ๋‘ ํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋•Œ ์ด ๋‘˜์€ 'id' ์ปฌ๋Ÿผ์„ ๊ณตํ†ต ์ปฌ๋Ÿผ์œผ๋กœ ํ•˜์—ฌ ํ•ฉ์ณ์•ผ ํ•˜๋Š”๋ฐ, ์ด๋•Œ ์“ฐ์ด๋Š” ๊ฒƒ์ด Join์ด๋ฉฐ ์šฐ๋ฆฌ๋Š” ์ด์ค‘์—์„œ๋„ Left join์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ฐ”๋กœ

๊ฐ ์‚ฌ์šฉ์ž์˜ ๊ณ ์œ  id๋ฅผ ์ถœ๋ ฅํ•˜๋˜, ๊ณ ์œ  id๊ฐ€ ์—†์„ ๊ฒฝ์šฐ null์„ ์ถœ๋ ฅํ•˜๋ผ

 

๋ผ๋Š” ์กฐ๊ฑด ๋•Œ๋ฌธ์ด๋‹ค.

 

์ด๋Š” ์ฆ‰, id๋Š” ์žˆ์–ด๋„ ๋˜๋‚˜ unique_id๋Š” ์—†์–ด๋„ ๋˜๋Š” ๊ฒƒ์œผ๋กœ, ๋ชจ๋“  ์ง์› ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” Employees ํ…Œ์ด๋ธ”์— unique_id๊ฐ€ ์žˆ๋Š” ์ง์›๋“ค๋งŒ id๋ฅผ ํ™•์ธํ•ด์„œ ์ฑ„์›Œ๋„ฃ์œผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

id name unique_id
1 Alice null
7 Bob null
11 Meir 2
90 Winston 3
3 Jonathan 1

 

id๊ฐ€ 1, 7์ธ ์ง์›๋“ค์€ unique_id๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ํ…Œ์ด๋ธ”์„ left joinํ•  ๊ฒฝ์šฐ ๊ฐ๊ฐ unique_id๊ฐ€ null๋กœ ํ‘œ๊ธฐ๋  ๊ฒƒ์ด๊ณ , ์ด ํ…Œ์ด๋ธ”์„ ๋ชจ๋‘ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ์œ„ ํ‘œ์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•