Identifying Earliest Product Sales | MySQL Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to find the product id, year, quantity, and price for the first year of every p
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to find the product id, year, quantity, and price for the first year of every product sold.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Find the earliest year each product was sold by grouping sales data.\n\n    > `MIN()` \u2192 This function returns the smallest value of the selected column\n\n    > `GROUP BY` \u2192 This command groups rows that have the same values into summary rows, typically to perform aggregate functions on them\n\n2. Select and display product details for those earliest years.\n\n    > `SELECT` \u2192 This command retrieves data from a database\n\n    > `AS` \u2192 This command renames a column with an alias (temporary name). In most database languages, we can skip the `AS` keyword and get the same result\n\n    > `IN` \u2192 This operator allows to specify multiple values\n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n`n` is the number of rows in the table. This is because the query processes each row in the table once to find the first year of every product sold.\n\n# Code\n```\nSELECT product_id, year AS first_year, quantity, price \nFROM Sales\nWHERE (product_id, year) IN (\n    SELECT product_id, MIN(year) \n    FROM Sales \n    GROUP BY product_id\n);\n```
['MySQL']