Tell me about a time you solved a complex SQL problem with multiple scenarios and edge cases.
For this SQL exercise, demonstrate that ability by reconciling eligible orders against settled payments and completed refunds. Handle unmatched records, duplicate payments, irrelevant statuses, zero values, and NULLs explicitly.
Output
- One row per customer and order month with a nonzero payment discrepancy.
- Columns:
customer_name, order_month, gross_order_value, paid_amount, refunded_amount, net_value, payment_gap, and discrepancy_rank.
- Ignore orders with a NULL customer or order date, and rows with NULL monetary values. Sort by customer ID and month.