select
    product_id,
    sum((unit_price - purchase_price) * quantity) as total_profit,
    round(
        avg(unit_price-purchase_price) / avg(purchase_price) * 100,
        2
    ) as profit_margin
from
    sales_orders
    join purchase_prices using (product_id)
where
    year(order_date) = 2023
group by
    product_id
order by
    product_id asc;