๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/sales-person/description/
Table: SalesPerson
Table: Company
Table: Orders
Write an SQL query to report the names of all the salespersons who did not have any orders related to the company with the name "RED".
Return the result table in any order.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
์ด๋ฆ์ด "RED"์ธ ํ์ฌ์ ๊ด๋ จ๋ ์ฃผ๋ฌธ์ด ์๋ ๋ชจ๋ ์์ ์ฌ์์ ์ด๋ฆ์ ์ถ๋ ฅํด์ผ ํ๋ค.
๋จ๊ณ1) ๊ฐ ํ ์ด๋ธ์ Primary key์ Foreign key ๊ด๊ณ๋ฅผ ๋ณด๊ณ ํ ์ด๋ธ์ ์กฐ์ธํ ํ, ํ์ฌ ์ด๋ฆ์ด 'RED'์ธ ํ์ฌ์ ์ฃผ๋ฌธ ๊ด๋ จ ์ด๋ ฅ์ด ์๋ ์์ ์ฌ์์ ์ด๋ฆ์ ์ถ๋ ฅ
๋จ๊ณ2) ํด๋น ์ถ๋ ฅ ์ฟผ๋ฆฌ๋ฅผ WHERE์กฐ๊ฑด์ผ๋ก ์ค์
SELECT name
FROM SalesPerson
WHERE name NOT IN (
SELECT SP.name AS name
FROM Orders O
LEFT JOIN Company C
ON O.com_id = C.com_id
LEFT JOIN SalesPerson SP
ON O.sales_id = SP.sales_id
WHERE C.name = 'RED'
)
โ ํ์ตํ๊ณ
์ค๋๋ง์ SQL๋ฌธ์ ํ์๋๋ easy ๋๋์๋ ํ์ฐธ ์๊ฐํ๋ ์์ ์ ๋ฐ๊ฒฌํจ..
๐ฑ ๋ชจ๋ ๋๊ตฌ๋ .. ๋ด๊ฒ์ผ๋ก ๋ง๋ค๊ธฐ ์ํด ๊พธ์คํ ๊พธ์คํ ๋ค๋ฃฐ ๊ฒ
๋ฆฌํธ์ฝ๋๋ ์ด๋ ค์ ๋ ๋ฌธ์ , ์๋ก์ด ๋ฌธ์ ๋ค ๊พธ์คํ ํ์ด๋๊ฐ์!