SIMPLE | USING CTE & MIN() & Group By & Inner Join
Complexity\nTime complexity: O(N)^2\nwhere N is the number of rows satisfying the conditions.\n\n# Space complexity: \nO(1)^2\n\n# Code\n\n# Write your MySQL qu
# Complexity\nTime complexity: O(N)^2\nwhere N is the number of rows satisfying the conditions.\n\n# Space complexity: \nO(1)^2\n\n# Code\n```\n# Write your MySQL query statement below\n\nWITH CTE AS (\n    SELECT product_id, MIN(year) AS minyear FROM Sales \n    GROUP BY product_id \n)\n\nSELECT s.product_id, s.year AS first_year, s.quantity, s.price \nFROM Sales s\nINNER JOIN CTE ON cte.product_id = s.product_id  AND s.year = cte.minyear; \n```\n![download (1).jpg](https://assets.leetcode.com/users/images/43c68b8a-1cb4-4c0a-855e-b2a0b06197c6_1713252773.0091515.jpeg)\n
['MySQL', 'MS SQL Server', 'PostgreSQL']