MYSQL : Two Solution - Window Function/ Subquery. Good Luck 👍✨
\n# Write your MySQL query statement below\n/**\nTwo Tables: Sales/Product\n\n(sale_id, year) is the primary key of Sales table.\nproduct_id is a foreign key to
```\n# Write your MySQL query statement below\n/**\nTwo Tables: Sales/Product\n\n(sale_id, year) is the primary key of Sales table.\nproduct_id is a foreign key to Product table.\nSales table shows a sale on the product product_id in a certain year.\nNote that the price is per unit.\n\nproduct_id is the primary key of Product table.\nProduct table indicates the product name of each product.\n\nPROBLEM: selects the product id, year, quantity, and price for the first year of every product sold\n*/\n#First Solution\n\nWITH CTE AS\n(\n    SELECT \n        product_id,\n        MIN(year) AS first_year\n    FROM Sales\n    GROUP BY 1\n)\nSELECT \n    product_id,\n    year AS first_year,\n    quantity,\n    price\nFROM Sales \nWHERE (product_id, year) in (select * from CTE)\n\n#Second Solution\n\nWITH CTE AS\n(\nSELECT\n    product_id,\n    year,\n    quantity,\n    price,\n    dense_rank() over(partition by product_id order by year asc) as rnk\nFROM Sales\n)\n\nSELECT\n     product_id,\n    year AS first_year,\n    quantity,\n    price\nFROM CTE where rnk = 1\n```
['MySQL']