Beats 76% | Simple CTE|
Intuition\nFirst we need to find first year per product_id then fetch the results.\n\n# Approach\nFor selecting first year we are using cte which is "with" to f
# Intuition\nFirst we need to find first year per product_id then fetch the results.\n\n# Approach\nFor selecting first year we are using cte which is "with" to find first year and grouping it with product_id.\nusing it i main query and join conditions where both product and year are matching will give us desired results.\n\n# Complexity\n- Time complexity:\neasy to write and fast\n\n- Space complexity:\nNo ambigious or complex syntaxes, simple exectuion of logic\n\n# Code\n```\n/* Write your PL/SQL query statement below */\nwith Start_year as (\n    SELECT product_id,MIN(year) as f_year\n    FROM SALES \n    GROUP BY product_id\n)\n SElECT s.product_id,sy.f_year as first_year,s.quantity,s.price\n from sales s\n join start_year sy\n on s.year = sy.f_year and s.product_id = sy.product_id\n```
['Oracle']