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_checkin 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.
| id | order_total | paid | refunded | payment_check |
|---|---|---|---|---|
| 113 | 249 | 0 | 0 | unpaid |
| 119 | 55 | 45 | 0 | underpaid |
| 121 | 45 | 0 | 0 | unpaid |
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 · key | shopper_idinteger → shoppers | order_datetext | statustext | delivered_datetext |
|---|---|---|---|---|
| 101 | 1 | 2025-01-04 | delivered | 2025-01-08 |
| 102 | 2 | 2025-01-06 | delivered | 2025-01-07 |
| 103 | 1 | 2025-01-15 | delivered | 2025-01-20 |
| 104 | 3 | 2025-01-18 | cancelled | NULL |
| 105 | 4 | 2025-01-22 | delivered | 2025-01-31 |
showing 5 of 33 rows
order_lines4 columns · 48 rows
| order_idinteger → sales_orders | item_idinteger → catalog_items | quantityinteger | unit_pricereal |
|---|---|---|---|
| 101 | 1 | 1 | 39 |
| 101 | 3 | 2 | 19.5 |
| 102 | 4 | 1 | 59 |
| 103 | 5 | 1 | 45 |
| 103 | 10 | 3 | 12 |
showing 5 of 48 rows
order_payments5 columns · 29 rows
| idinteger · key | order_idinteger | paid_attext | amountreal | methodtext |
|---|---|---|---|---|
| 1 | 101 | 2025-01-04 | 78 | card |
| 2 | 102 | 2025-01-06 | 59 | paypal |
| 3 | 103 | 2025-01-15 | 81 | card |
| 4 | 105 | 2025-01-22 | 90 | card |
| 5 | 105 | 2025-01-23 | 38 | gift_card |
showing 5 of 29 rows
order_refunds4 columns · 6 rows
| idinteger · key | order_idinteger | refunded_attext | amountreal |
|---|---|---|---|
| 1 | 103 | 2025-01-25 | 15 |
| 2 | 106 | 2025-02-06 | 74 |
| 3 | 111 | 2025-02-25 | 59 |
| 4 | 111 | 2025-02-26 | 45 |
| 5 | 122 | 2025-03-20 | 10 |
showing 5 of 6 rows
Hints and the answer are always available. Using them counts the question as solved with help.