DEVELOPED COURSE TEXT
A business query as an executable, reconciled claim
Write the metric contract in plain language
For this example, select orders currently marked paid whose order creation date is from January 1 inclusive to January 6 exclusive. Sum recorded payments and subtract recorded refunds for those selected orders. This is net recorded cash for an order cohort in the fixture snapshot. It is not a cash-flow statement for the receipt period, not net profit and not automatically accounting revenue. Payment/refund event dates, tax, fees and other accounting facts are absent.
Build one row per order before summing
The common-table expressions independently aggregate payments and refunds to order grain. The selected-order set is then left joined to each summary. COALESCE is used because this fixture contract treats absent recorded child events as zero while separate completeness controls remain necessary.
WITH paid_orders AS (
SELECT order_id, customer_id FROM orders
WHERE status = 'paid'
AND created_date >= '2026-01-01' AND created_date < '2026-01-06'
), payment_totals AS (
SELECT order_id, SUM(amount_cents) AS paid_cents
FROM payments GROUP BY order_id
), refund_totals AS (
SELECT order_id, SUM(amount_cents) AS refunded_cents
FROM refunds GROUP BY order_id
)
SELECT o.order_id, COALESCE(p.paid_cents,0) AS paid_cents,
COALESCE(r.refunded_cents,0) AS refunded_cents,
COALESCE(p.paid_cents,0)-COALESCE(r.refunded_cents,0) AS net_cash_cents
FROM paid_orders AS o
LEFT JOIN payment_totals AS p ON p.order_id=o.order_id
LEFT JOIN refund_totals AS r ON r.order_id=o.order_id
ORDER BY o.order_id;
Reconcile independent quantities before trusting the dashboard
The following control looks for currently paid orders whose recorded line amount differs from recorded payment total. It returns no rows in the starting fixture. That is one passed invariant, not proof of every financial assertion. Refunds are deliberately not subtracted in this gross-payment reconciliation; they are a separate event used in the net-cash metric. A real system must define how partial, excess and reversed payments affect status and reporting.
WITH line_totals AS (
SELECT order_id,SUM(quantity*unit_price_cents) AS line_cents
FROM order_items GROUP BY order_id
), payment_totals AS (
SELECT order_id,SUM(amount_cents) AS paid_cents
FROM payments GROUP BY order_id
)
SELECT o.order_id,COALESCE(l.line_cents,0),COALESCE(p.paid_cents,0)
FROM orders AS o
LEFT JOIN line_totals AS l ON l.order_id=o.order_id
LEFT JOIN payment_totals AS p ON p.order_id=o.order_id
WHERE o.status='paid' AND COALESCE(l.line_cents,0)<>COALESCE(p.paid_cents,0)
ORDER BY o.order_id;
Test boundaries and counterexamples
Useful tests include no matching rows, zero-order customers, multiple items, split payments, equal-valued independent payments, refunds and changes at the date boundaries. A query that works only on one-row-per-parent data has not been tested for its intended relationships. Preserve the fixture, expected outputs and engine version so another reader can reproduce both the correct result and the known failure examples. A green test set supports the covered cases; it does not establish all future data completeness or semantic correctness.
Worked records
Record 1
The defined paid-order cohort contains orders 1, 3 and 5. Payments are 2,500, 2,100 and 1,000 cents. Order 3 has a 700-cent refund.
Develop the reasoning
The one-row-per-order results are (1,2500,0,2500), (3,2100,700,1400) and (5,1000,0,1000). Gross recorded payments total 5,600, refunds 700 and net recorded cohort cash 4,900 cents. Independently, gross paid-order line amounts also total 5,600, so the starting reconciliation query reports no mismatches. These checks answer different questions: a gross reconciliation can pass while net cash changes through a valid refund. If a payment date were later than the order cohort, this fixture still cannot report which calendar period received the cash. The conclusion must retain the cohort and snapshot qualifications.
Progressive practice — optional
1. Foundation
Does 4,900 cents establish net profit?
Compare the worked reasoning
No. Costs, expenses, recognition rules, taxes and other facts are absent.
Change the record
The manager renames the chart profit without changing the SQL.
Compare the changed-record reasoning
The numerical result does not change meaning because its title changed; correct the metric contract or supply the needed accounting model.
2. Application
A second 700-cent refund is legitimately recorded for order 3. What is the modeled cohort net?
Compare the worked reasoning
It becomes 5,600−1,400=4,200 cents, assuming no other changes.
Change the record
An analyst uses SUM(DISTINCT refund amount).
Compare the changed-record reasoning
That collapses legitimate equal-valued refund events and incorrectly preserves the older total.
3. Transfer
The gross line/payment reconciliation returns no rows. Does that prove every payment source is complete?
Compare the worked reasoning
No. Matching incomplete or incorrectly associated sources can still agree.
Change the record
An independent ingestion audit finds a missing transaction batch.
Compare the changed-record reasoning
Reconstruct completeness and rerun the defined controls; equality of two derived totals did not certify their underlying source coverage.
Sources and scope
- SQLite documentation: SQL language behavior for the explicitly named SQLite examples; portability to another engine requires checking that engine.
- SQLite — Built-in Aggregate Functions: COUNT, SUM, DISTINCT and empty-input behavior.