๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ 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 |