관리 메뉴

오늘도 배운다

1070. Product Sales Analysis III / LeetCode, SQL, MySQL 본문

코딩테스트연습(SQL)

1070. Product Sales Analysis III / LeetCode, SQL, MySQL

LearnerToRunner 2023. 4. 10. 22:48

문제

source: LeetCode
Write an SQL query that selects the product id, year, quantity, and price for the first year of every product sold.
Return the resulting table in any order.

 

 

 

 

제출답안(MySQL)

SELECT
    product_id, year AS first_year, quantity, price
FROM
    sales
WHERE
    (product_id, year) IN (SELECT product_id, MIN(year) as first_year
                            FROM sales
                            GROUP BY product_id)

 

 

 

 

문제 바로가기(MySQL)

728x90
Comments