← SQL Operations Cases

Final project · SQLite

Deliver regional net revenue

My saved work →

Your work

Active 0:00 · Paused 0s

Show the reference answer
WITH r AS (SELECT order_id,SUM(refund) AS refunded FROM returns GROUP BY order_id) SELECT o.region,SUM(o.amount-COALESCE(r.refunded,0)) AS net_revenue FROM orders o LEFT JOIN r ON o.order_id=r.order_id WHERE o.status='paid' GROUP BY o.region ORDER BY net_revenue DESC, o.region ASC

Avoid duplicating sales with multiple refunds. East net is 270, West 120, North 30.