CODE WORKSPACE

Order anomaly audit

Write a query that returns one row per detected order anomaly across duplicate orders, suspicious revenue, and invalid order-state transitions.

Break each anomaly type into a named CTE, then union the findings into a stable two-column audit result.

Sample input

ordersOrder-level revenue facts with duplicate rows and suspicious amounts.
order_idcustomer_idstatusgross_revenuerefund_amount
100110placed1200
100110placed1200
100211paid-250
100312refunded8090
order_eventsLifecycle events used to detect impossible state transitions.
order_idevent_tsevent_name
10042026-03-01T12:55:00Zcancelled
10042026-03-01T12:57:00Zpaid
10052026-03-01T13:30:00Zplaced
10052026-03-01T13:31:00Zpaid

Expected output

Expected output shapeExample anomaly findings. Your final query should return all matching findings from the sandbox.
order_idanomaly_type
1001duplicate_order
1002negative_or_over_refund
1003negative_or_over_refund
1004paid_after_cancelled

Constraints

Treat duplicate order rows, negative gross revenue, refunds larger than gross revenue, and paid events after cancellation as audit findings.

Expected skills

CTE decomposition, row-level data quality checks, event ordering, and audit output that is easy to review.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.