๐ป ๋ฌธ์ ์ฃผ์ : Product Sales Analysis I - LeetCode
Product Sales Analysis I - LeetCode
Can you solve this real interview question? Product Sales Analysis I - Table: Sales +-------------+-------+ | Column Name | Type | +-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | +-----------
leetcode.com
๐ป ๋ฌธ์
Write a solution to report the product_name, year, and price for each sale_id in the Sales table. Return the resulting table in any order.
- Sales ํ ์ด๋ธ ๋ด ๊ฐ sale_id๋ณ๋ก product_name, year, ๊ทธ๋ฆฌ๊ณ price๋ฅผ ์ถ๋ ฅํ์์ค. ์ถ๋ ฅ ์์๋ ์๊ด ์์ต๋๋ค.
๐ป ์ฝ๋ ๋ฐ ํ์ด
select p.product_name, s.year, s.price
from Sales s
left join Product p on s.product_id = p.product_id
์ ๋ฌธ์ ์ ๊ฒฝ์ฐ, ๊ฒฐ๊ตญ ๊ตฌํด์ผ ํ๋ ์ปฌ๋ผ์ 'product_name, year, price'์ด๋ค. ์ด ์ธ ๊ฐ์ง ์ค ๋ ๊ฐ์ง๊ฐ Sales ํ ์ด๋ธ์ ์๊ธฐ ๋๋ฌธ์ ํ ํ ์ด๋ธ์ ์ปฌ๋ผ์ ๋ชฐ์์ ๋ถ์ด๊ธฐ ์ํด left join์ ์ฌ์ฉํ์๋ค.
Sales ํ ์ด๋ธ๊ณผ Product ํ ์ด๋ธ์ ๊ณตํต ์ปฌ๋ผ? product_id
๋คํํ ์ด ๋ ํ ์ด๋ธ์ product_id๋ผ๋ ๊ณตํต ์ปฌ๋ผ์ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ ์ด๋ฅผ join์ ์กฐ๊ฑด์ผ๋ก ํ์ฌ left join์ ํด์ฃผ๋ฉด ๊น๋ํ๊ฒ ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋๋ค.
'SQL > LeetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode][SQL50] #9. Rising Temperature (0) | 2023.11.08 |
---|---|
[LeetCode] (์๋ธ์ฟผ๋ฆฌ, ์กฐ์ธ ์๋) #8. Customer Who Visited but Did Not Make Any Transactions (1) | 2023.11.08 |
[LeetCode][SQL50] #6. Replace Employee ID With The Unique Identifier (0) | 2023.11.07 |
[LeetCode][SQL50] #5. Invalid Tweets (0) | 2023.11.06 |
[LeetCode][SQL50] #4. Article Views I (0) | 2023.11.06 |