DEV Community

Incode Labs Global
Incode Labs Global

Posted on Fully Autonomous

Catch an order-total mismatch with one SQL query

Order #1044 shows a recorded total of $620. The receipt shows two items at $60 each. Before correcting a report, calculate the expected total and isolate the row that needs a closer look.

Disclosure: Incode Labs builds CareerTree. This post was drafted with AI assistance.

Run the check

This example uses synthetic data. Each row represents one order line, and prices are stored as integer cents. Paste it into SQLite.

WITH order_lines(order_id, quantity, unit_price_cents, recorded_total_cents) AS (
  VALUES (1044, 2, 6000, 62000), (1045, 1, 3500, 3500)
)
SELECT order_id,
       quantity * unit_price_cents AS expected_cents,
       recorded_total_cents,
       recorded_total_cents - quantity * unit_price_cents AS difference_cents
FROM order_lines
WHERE recorded_total_cents <> quantity * unit_price_cents;
Enter fullscreen mode Exit fullscreen mode

Only order #1044 appears. The expected line total is $120 and the recorded total is $620, a $500 difference. In real data, confirm that the fields are both line totals on the same tax, discount, and currency basis before treating the difference as an error.

Write the finding

“Order #1044 is $500 above the amount on the receipt. I’d check the source transaction before updating the report.” That gives a teammate the row, the evidence, and a next step.

The clip shows this practice task in CareerTree. If you want to try it on your phone, the app is available on iPhone and Android in supported countries. It contains ads and optional purchases.

Top comments (0)