← SQL Operations Cases

Independent practice · SQLite

Calculate net paid amounts

My saved work →

Your work

Active 0:00 · Paused 0s

Show 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.