🔥🔥🔥 3 solutions in MySQL | Beats 66.61%🔥🔥🔥
1. GROUP BY & Sub-query with 1080ms Runtime (Beats 66.61%)\n- Time complexity: O(n log n) + O(NM) (with indexing optimizations)\n- Space complexity: O(m)\n\n# C
# 1. GROUP BY & Sub-query with 1080ms Runtime (Beats 66.61%)\n- **Time complexity:** O(n log n) + O(N*M) (with indexing optimizations)\n- **Space complexity:** O(m)\n\n# Code\n```mysql []\nSELECT \n    product_id,\n    year AS first_year,\n    quantity ,\n    price\nFROM sales \nWHERE (product_id, year) IN (\n        SELECT \n            product_id,\n            MIN(year) AS first_year\n        FROM sales\n        GROUP BY product_id\n);\n```\n\n# 2. Window function with 1094ms Runtime (Beats 63.36%)\n- **Time complexity:** O(n log n)\n- **Space complexity:** O(n)\n\n# Code\n```mysql []\nSELECT \n    res.product_id,\n    res.min_year AS first_year,\n    res.quantity,\n    res.price\nFROM (\n    SELECT \n        a.*,\n        MIN(year) OVER (PARTITION BY product_id) AS min_year\n    FROM sales a\n) res\nWHERE res.year = res.min_year;\n```\n\n# 3. CTE & JOIN with 1091ms Runtime (Beats 64.01%)\n- **Time complexity:** O(n)\n- **Space complexity:** O(n)\n\n# Code\n```mysql []\nWITH min_year_cte AS (\n    SELECT \n        product_id,\n        MIN(year) AS min_year\n    FROM sales\n    GROUP BY product_id\n)\nSELECT \n    s.product_id,\n    my.min_year AS first_year,\n    s.quantity,\n    s.price\nFROM sales s\nJOIN min_year_cte my ON s.product_id = my.product_id AND s.year = my.min_year;\n```
['MySQL']