๐ป ๋ฌธ์ ์ฃผ์ : 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์ ์ถ๋ ฅํ๋๋ก ํ์์ค. ์ถ๋ ฅ ์์๋ ์๊ด ์์ต๋๋ค.

๐ป ์ฝ๋ ๋ฐ ํ์ด
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๋ก ํ๊ธฐ๋ ๊ฒ์ด๊ณ , ์ด ํ ์ด๋ธ์ ๋ชจ๋ ์ถ๋ ฅํด๋ณด๋ฉด ์ ํ์ ๊ฐ์ด ๊ตฌ์ฑ๋์ด ์์์ ํ์ธํ ์ ์๋ค.
'SQL > LeetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] (์๋ธ์ฟผ๋ฆฌ, ์กฐ์ธ ์๋) #8. Customer Who Visited but Did Not Make Any Transactions (1) | 2023.11.08 |
---|---|
[LeetCode][SQL50] #7. Product Sales Analysis I (0) | 2023.11.07 |
[LeetCode][SQL50] #5. Invalid Tweets (0) | 2023.11.06 |
[LeetCode][SQL50] #4. Article Views I (0) | 2023.11.06 |
[LeetCode][SQL50] #3. Big Countries (0) | 2023.11.06 |