select s.product_id,sum((unit_price-purchase_price)*quantity) as total_profit,
 round(avg((unit_price-purchase_price))/purchase_price*100,2) as profit_margin
from sales_orders s
join purchase_prices p on p.product_id=s.product_id
where order_date >= '2023-01-01' AND order_date <= '2023-12-31'
group by s.product_id
order by s.product_id;