runtime
48 / 88

task Reconciliation & quality gates · hard

Reconcile orders, payments and refunds

For every non-cancelled order, compute order_total (lines), paid (payments) and refunded (refunds), and classify payment_check: 'unpaid' when nothing was paid, 'underpaid' when paid < order_total, 'overpaid' when paid > order_total, otherwise 'ok'. Return id, order_total, paid, refunded and payment_check for orders that are not 'ok'.

what to return

Columns
idorder_totalpaidrefundedpayment_check in this order
Rows
5 rows · any order
Watch for
Missing payments or refunds count as 0.
Round order_total, paid and refunded to 2 decimals and classify using the rounded values.
Refunds don't change payment_check (a refund is a separate event).
Skill
Reconcile several sources without double counting and classify mismatches. Refresher: Multi-grain joins without fan-out →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result5 rows · any order
idorder_totalpaidrefundedpayment_check
11324900unpaid
11955450underpaid
1214500unpaid

first 3 of 5 rows

data you can query

Sample rows from each table. Each column shows its type underneath; → marks a link to another table.

sales_orders5 columns · 33 rows
idinteger · keyshopper_idinteger → shoppersorder_datetextstatustextdelivered_datetext
10112025-01-04delivered2025-01-08
10222025-01-06delivered2025-01-07
10312025-01-15delivered2025-01-20
10432025-01-18cancelledNULL
10542025-01-22delivered2025-01-31

showing 5 of 33 rows

order_lines4 columns · 48 rows
order_idinteger → sales_ordersitem_idinteger → catalog_itemsquantityintegerunit_pricereal
1011139
1013219.5
1024159
1035145
10310312

showing 5 of 48 rows

order_payments5 columns · 29 rows
idinteger · keyorder_idintegerpaid_attextamountrealmethodtext
11012025-01-0478card
21022025-01-0659paypal
31032025-01-1581card
41052025-01-2290card
51052025-01-2338gift_card

showing 5 of 29 rows

order_refunds4 columns · 6 rows
idinteger · keyorder_idintegerrefunded_attextamountreal
11032025-01-2515
21062025-02-0674
31112025-02-2559
41112025-02-2645
51222025-03-2010

showing 5 of 6 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.