← SQL Operations CasesMy saved work →
Independent practice · SQLite
Calculate net paid amounts
Your work
Active 0:00 · Paused 0sShow the reference answer
WITH r AS (SELECT order_id,SUM(refund) refund FROM returns GROUP BY order_id) SELECT o.order_id,o.amount-COALESCE(r.refund,0) AS net_amount FROM orders o LEFT JOIN r ON o.order_id=r.order_id WHERE o.status='paid'
A direct join repeats the sale for each refund. Order 1 net is 90.