본문 바로가기
[업무 지식]/MySQL

[where & in] Product Sales Analysis III

by 에디터 윤슬 2024. 11. 1.
 

링크

https://leetcode.com/problems/product-sales-analysis-iii/description/

문제

Write a solution to select the product id, year, quantity, and price for the first year of every product sold.

Return the resulting table in any order.

The result format is in the following example.

정답

select product_id,
        year as first_year,
        quantity,
        price
from sales
where (product_id, year) in (select product_id,
                                    min(year)
                             from sales
                            group by product_id)

오답

select p.product_id,
        min(year) as first_year,
        quantity,
        price
from product p
join sales s
on p.product_id = s.product_id
group by p.product_id

새롭게 이해한 내용

  • 조건은 조건에 해당하는 게 정확하다