Easy Solution using RANK 🔥
ApproachThe first step is to join the sales and product tables based on the product_id. This ensures we retrieve the required columns: product_id, year, quantit

# Approach
<!-- Describe your approach to solving the problem. -->
The first step is to join the sales and product tables based on the product_id. This ensures we retrieve the required columns: product_id, year, quantity, and price.

**Ranking with Window Function:**
Use the RANK() function with a PARTITION BY product_id and ORDER BY year ASC to rank rows for each product. This helps in identifying the first year for each product.

**Filter the First Year:**
In the final step, filter only the rows where the rank is 1, as these correspond to the earliest year for each product.


# Code
```postgresql []
-- Write your PostgreSQL query statement below
with cte as (
    select s.product_id as product_id , year , quantity , price 
    from sales s join
    product p on s.product_id = p.product_id
)
select product_id , year as first_year , quantity , price from (
    select *,
    rank() over (partition by product_id order by year)
    from cte
) as x where x.rank = 1; 
```
['PostgreSQL']