QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › SQL in QA work

How do you check that a balance matches the transaction history?

A balance is reconciled with a query that sums each account's completed operations and returns accounts where the sum differs from the stored balance. A normal result is empty. To avoid losing accounts it uses LEFT JOIN with the status condition inside the join, COALESCE and IS DISTINCT FROM instead of <>. The requirements settle whether pending operations count.

SQL · QA practice

The UI shows a balance of 80.00, while the transaction history adds up to 70.00. Which of the two is wrong?

A balance is stored in a separate column so as not to recount the history each time, and the transaction history remains the source of truth. While both numbers change correctly, they match. After a race, a partial failure or a manual edit in the database they diverge, and the user sees an amount no operation confirms. This lesson is about reconciling the stored value with what follows from the history, and about the query details on which it quietly breaks.

How it works and why

A stored balance is a derived value: balance = the sum of completed deposits minus the sum of completed withdrawals. A property that must always hold is called an invariant. Checking an invariant is a query that for each account computes the sum from the history and returns only the accounts where it differs from the stored value. Normally the result is empty. Each element of the query answers a specific danger: LEFT JOIN (or a subquery with LEFT JOIN) so as not to lose accounts with no operations, the status condition inside the join or the subquery so as not to lose them either, COALESCE because the sum of an empty set is NULL, and IS DISTINCT FROM instead of <> because a comparison with NULL is never true.

Example: two mismatches

Account balances: 1 is 150.00, 2 is 80.00, 3 is 0.00 (no operations), 4 is 20.00. The history is in the table below.

SELECT a.id, a.balance, COALESCE(s.total, 0) AS calculated
FROM accounts a
LEFT JOIN (
  SELECT account_id,
         SUM(CASE type WHEN 'deposit' THEN amount
                       ELSE -amount END) AS total
  FROM transactions
  WHERE status = 'completed'
  GROUP BY account_id
) s ON s.account_id = a.id
WHERE a.balance IS DISTINCT FROM COALESCE(s.total, 0);

The history and the reconciliation result

transactions
accounttypeamountstatus
1deposit200.00completed
1withdrawal50.00completed
2deposit100.00completed
2withdrawal30.00completed
4deposit20.00pending
Query result
idbalancecalculated
280.0070.00
420.000
Figure 1. Account 1 matches (200 − 50 = 150), and account 3 with no operations too, so they are not in the result.

Account 2: the history gives 100 − 30 = 70, while 80 is stored. The difference of 10.00 means a lost withdrawal row, a double credit or a manual edit: the cause is sought in the change log of this account. Account 4: the balance is 20.00 and there are no completed operations, only a deposit with status pending. Whether this is a defect depends on the rule: if the balance must count only completed operations, it is a bug, if it reserves expected ones, the rule is different. So the question "what to count" is clarified in the requirements before running the query.

A variant with the same expression repeated twice in HAVING works too, but the subquery reads better and does not multiply rows if another table is joined to accounts later.

Where the status condition goes

Wrong

in WHERE after LEFT JOIN

Accounts with no completed operations are filtered out and the mismatch of account 4 disappears.

Right

in the subquery or in ON

All accounts stay, a sum with no operations becomes 0.

Figure 2. The same rule as for any LEFT JOIN: a condition on the right table does not go in WHERE.

Limits and alternatives

Reconciling during writes gives false mismatches: a transaction has already inserted a row but not yet updated the balance. The query is run on a "quiet" environment, in a transaction with the REPEATABLE READ level (one data snapshot) or with a time cutoff. The expression ELSE -amount treats any type other than deposit as a withdrawal, so before reconciling check which type values exist: a fee, a refund or an adjustment may have the opposite sign. To find the operation where the mismatch arose, compute a running total with a window function. On large tables aggregating the whole history is slow: in a test, reconcile a single account.

Common mistakes

  • JOIN instead of LEFT JOIN: accounts with no operations vanish, and with them possible defects.
  • The status condition in WHERE after LEFT JOIN.
  • No COALESCE: balance <> NULL is never true, and an account with no operations quietly drops out.
  • Counting all types as withdrawals without looking at the list of types.
  • Running reconciliation under load and treating a race as a data defect.

Check yourself

Why does the reconciliation use IS DISTINCT FROM and not <>?

Answer

If the balance column contains NULL, NULL <> 0 gives UNKNOWN and the row does not reach the result. IS DISTINCT FROM treats NULL as a value of its own and shows such an account.

Reconciliation showed an account with a balance of 20.00 and a single deposit with status pending. Is it a bug?

Answer

It depends on the rule: if the balance must count only completed operations, yes, a defect. If expected credits are reserved, the reconciliation rule must change. The question is clarified in the requirements, not guessed.

In an interview. Junior: I sum the completed transactions of each account with their direction and compare with the stored balance. The query returns only mismatches, normally it is empty. Middle/Senior: The details matter: LEFT JOIN so as not to lose accounts with no operations, the status condition not in WHERE, COALESCE for an empty sum and IS DISTINCT FROM for NULL. I reconcile on a data snapshot or without load, check the list of operation types and clarify whether pending is part of the balance.

More articles in this chapter