Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Return order_id, order_total, items_total, and diff for every order that has item rows.
Result columns · in this order
order_idorder_totalitems_totaldiffHow to approach it
Build item_totals first, then join back to orders.
Sample input
| order_id | order_total | example_item_rows |
|---|---|---|
| 1001 | 80 | SKU-A 80, SKU-B 30, SKU-C 120 |
| 1004 | 220 | SKU-A 80, SKU-D 60, SKU-E 80 |
2 rows — all rows shown.
Expected output
| order_id | order_total | items_total | diff |
|---|---|---|---|
| 1001 | 80 | 230 | -150 |
| 1002 | 120 | 140 | -20 |
| 1004 | 220 | 220 | 0 |
3 rows — all rows shown.
Constraints
Aggregate order_items to one row per order before joining to orders. diff is orders.total_amount - items_total. Order by order_id.
Expected skills
Fanout prevention, pre-aggregation, and reconciliation.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.