SQL: Identifying Initial Sales Data for Products
Code\n\nSELECT product_id, year AS first_year, quantity, price\nFROM sales\nWHERE (product_id, year) IN \n        (SELECT product_id, MIN(year) FROM sales GROUP
# Code\n```\nSELECT product_id, year AS first_year, quantity, price\nFROM sales\nWHERE (product_id, year) IN \n        (SELECT product_id, MIN(year) FROM sales GROUP BY product_id)\n```\n# Complexity\n- Time complexity: $$O(n * logn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity is influenced by the size of the sales table. The GROUP BY operation in the subquery and the IN clause in the main query both contribute to the overall complexity. Assuming efficient indexing, the complexity would likely be O(n log n), where n is the number of rows in the sales table.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(m)$$ -->\nThe space complexity is mainly determined by the size of the data returned by the subquery. It should be relatively low, as the subquery only returns a list of product_id and their corresponding minimum year. Thus, it can be considered O(m), where m is the number of distinct products.\n![upvote_leetcode.png](https://assets.leetcode.com/users/images/e920c0d4-6675-4623-85f8-1ded83e5d800_1702916037.978024.png)\n\n\n
['MySQL']