CODE WORKSPACE

Interview fanout-safe order revenue

Return order_id, order_total, items_total, and diff after aggregating order_items to order grain before joining.

Start by naming the output grain, then build CTEs that can be inspected one at a time.

Sample input

orders joined to pre-aggregated item totalsAggregate order_items to one row per order before comparing to orders.total_amount.
order_idorder_totalexample_item_rows
100180SKU-A 80, SKU-B 30, SKU-C 120
1004220SKU-A 80, SKU-D 60, SKU-E 80

Expected output

Expected outputFirst rows show mismatches caused by recomputing from item grain safely.
order_idorder_totalitems_totaldiff
100180230-150
1002120140-20
10042202200

Constraints

State the output contract first, use readable CTE layers, return the exact columns requested by the prompt, and use deterministic ordering.

Expected skills

Interview clarification, SQL structure, edge-case handling, and final-answer narration.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.