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