select
    s.product_id,
    sum((unit_price - purchase_price) * quantity) total_profit,
    round(
        avg((unit_price - purchase_price) / purchase_price) * 100,
        2
    ) profit_margin
from
    sales_orders s
    join purchase_prices p on s.product_id = p.product_id
where
    substring(order_date, 1, 4) = '2023'
group by
    1
order by
    1