๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL/LeetCode

[LeetCode][SQL50] #7. Product Sales Analysis I

by ์ด๋ฎด 2023. 11. 7.
728x90

๐Ÿ’ป ๋ฌธ์ œ ์ฃผ์†Œ : 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๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค. ์ถœ๋ ฅ ์ˆœ์„œ๋Š” ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค.

Input๊ณผ Output ์˜ˆ์‹œ

 

๐Ÿ’ป ์ฝ”๋“œ ๋ฐ ํ’€์ด

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์„ ํ•ด์ฃผ๋ฉด ๊น”๋”ํ•˜๊ฒŒ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•