select
    t1.product_id 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 t1 join purchase_prices t2 on t1.product_id = t2.product_id
where
    order_date between '2023-01-01' and '2023-12-21'
group by
    t1.product_id
order by
    t1.product_id