Step by step solution development | Easy to understand
Steps: \n1. Find the First Year of Sale for Each Product\n2. Product Details for the First Year of Every Product Sold\n3. Summary\n\n# Code\n\nSELECT product_id
# Steps: \n1. Find the First Year of Sale for Each Product\n2. Product Details for the First Year of Every Product Sold\n3. Summary\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```\n```\nSales table:\n+---------+------------+------+----------+-------+\n| sale_id | product_id | year | quantity | price |\n+---------+------------+------+----------+-------+ \n| 1       | 100        | 2008 | 10       | 5000  |\n| 2       | 100        | 2009 | 12       | 5000  |\n| 7       | 200        | 2011 | 15       | 9000  |\n+---------+------------+------+----------+-------+\nProduct table:\n+------------+--------------+\n| product_id | product_name |\n+------------+--------------+\n| 100        | Nokia        |\n| 200        | Apple        |\n| 300        | Samsung      |\n+------------+--------------+\n```\n\n## Step 1: Find the First Year of Sale for Each Product\n```\nSELECT \n    product_id,\n    MIN(year) AS first_year\nFROM \n    Sales\nGROUP BY \n    product_id;\n```\n\n## Result:\n\n```\n+------------+------------+\n| product_id | first_year  |\n+------------+------------+\n| 100        | 2008       |\n| 200        | 2011       |\n+------------+------------+\n```\n\n## Step 2: Select the Product Details for the First Year of Every Product Sold\n```\nSELECT \n    product_id,\n    year AS first_year,\n    quantity,\n    price\nFROM \n    Sales\nWHERE \n    (product_id, year) IN \n    (\n        -- Subquery: First year of sale for each product\n        SELECT \n            product_id,\n            MIN(year) AS first_year\n        FROM \n            Sales\n        GROUP BY \n            product_id\n    );\n```\n\n## Result:\n\n```\n+------------+------------+----------+-------+\n| product_id | first_year  | quantity | price |\n+------------+------------+----------+-------+\n| 100        | 2008       | 10       | 5000  |\n| 200        | 2011       | 15       | 9000  |\n+------------+------------+----------+-------+\n```\n\n## Summary: \nThus final query selects the `product id, year, quantity, and price` for the `first year of every product sold`, based on the results obtained from the subquery. \n\nIn above example, it shows the details for the first year of sale for products with ids 100 and 200.
['MySQL']