Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[LeetCode] 607. Sales Person

์ธ” 2023. 4. 20. 17:57

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

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


โ–  ๋ฌธ์ œ

https://leetcode.com/problems/sales-person/description/

 

Sales Person - LeetCode

Can you solve this real interview question? Sales Person - Table: SalesPerson +-----------------+---------+ | Column Name | Type | +-----------------+---------+ | sales_id | int | | name | varchar | | salary | int | | commission_rate | int | | hire_date |

leetcode.com

 

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 ๋‚œ๋„์—๋„ ํ•œ์ฐธ ์ƒ๊ฐํ•˜๋Š” ์ž์‹ ์„ ๋ฐœ๊ฒฌํ•จ..

๐ŸŒฑ ๋ชจ๋“  ๋„๊ตฌ๋Š” .. ๋‚ด๊ฒƒ์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๊พธ์ค€ํžˆ ๊พธ์ค€ํžˆ ๋‹ค๋ฃฐ ๊ฒƒ

๋ฆฌํŠธ์ฝ”๋“œ๋„ ์–ด๋ ค์› ๋˜ ๋ฌธ์ œ, ์ƒˆ๋กœ์šด ๋ฌธ์ œ๋“ค ๊พธ์ค€ํžˆ ํ’€์–ด๋‚˜๊ฐ€์ž!