In two sentences
A data test is a query that returns the rows breaking a rule: no rows means the test passes. Reconciliation compares the same numbers in two places (a source and your table, a payment provider and the ledger) and lists every difference.
See it
Five tests on a small customers and orders, each counting its failing rows:
| test | failing rows |
|---|---|
customers.customer_id is unique | 1 (c2 appears twice) |
customers.email is not null | 1 |
orders.customer_id exists in customers | 1 (c9) |
orders.status is one of the accepted values | 1 (lost) |
orders loaded in the last 24 hours | 0 |
And a reconciliation between the payment provider (PSP) and the ledger:
| payment_id | result |
|---|---|
| p1 | ok |
| p2 | amount differs (50.00 vs 49.50) |
| p3 | missing in ledger |
| p4 | missing at PSP |
In SQL
The five tests, as one results table:
SELECT 'customers.customer_id is unique' AS test, count(*) AS failing
FROM (SELECT customer_id FROM customers GROUP BY customer_id HAVING count(*) > 1)
UNION ALL
SELECT 'customers.email is not null', count(*) FROM customers WHERE email IS NULL
UNION ALL
SELECT 'orders.customer_id exists in customers', count(*)
FROM orders o WHERE NOT EXISTS (SELECT 1 FROM customers c WHERE c.customer_id = o.customer_id)
UNION ALL
SELECT 'orders.status in accepted values', count(*)
FROM orders WHERE status IS NULL OR status NOT IN ('pending', 'paid', 'shipped', 'refunded')
UNION ALL
SELECT 'orders loaded in the last 24 hours',
CASE WHEN max(loaded_at) >= localtimestamp - INTERVAL '24' HOUR THEN 0 ELSE 1 END
FROM orders;Reconciliation: a FULL OUTER JOIN keeps rows found on either side only.
SELECT coalesce(p.payment_id, l.payment_id) AS payment_id,
CASE WHEN l.payment_id IS NULL THEN 'missing in ledger'
WHEN p.payment_id IS NULL THEN 'missing at PSP'
WHEN abs(p.amount - l.amount) > 0.01 THEN 'amount differs'
ELSE 'ok' END AS result
FROM psp p
FULL OUTER JOIN ledger l ON l.payment_id = p.payment_id;When to use it
| where | what to test | if it fails |
|---|---|---|
| right after landing raw data | row counts against the source, schema, freshness | alert; don't build on top |
| on cleaned tables | unique keys, not-null keys, accepted values, referential integrity | stop the pipeline |
| before publishing (write-audit-publish) | business rules and totals against the source of truth | don't publish; quarantine |
| on reports people use | reconciliation with finance or the source system | flag the report as unverified |
Best practices
- Every table has at least: unique key, not-null key, freshness. Most incidents are caught by these three.
- A test returns the failing rows, not just a count. Store a sample of them; "1 failing row" without the row wastes the morning.
- Decide severity up front: some tests stop the pipeline (duplicate keys in a fact), some only warn (a new status value).
- Reconcile with a tolerance, and say which. Money rounds differently in different systems; 0.01 per payment and an agreed total tolerance.
- Test the fix, not just the bug. When an incident is fixed, add the test that would have caught it.
Common mistakes
NOT INin an accepted-values test.status NOT IN (…)is unknown for a NULL status, so NULLs pass. Teststatus IS NULL OR status NOT IN (…), or decide NULL is allowed, explicitly.- Reconciling with an INNER JOIN. Rows missing on one side disappear: exactly the ones you were looking for.
- Comparing totals only. Two errors of +10 and −10 cancel out. Reconcile per key, then the totals.
- Freshness from the event time. Late events have old timestamps; test when the data was loaded.
- Tests nobody reads. A failing test that doesn't stop anything or page anyone will be ignored. Wire it to the pipeline.
What an interviewer asks next
- "Which tests would you put on this table?" Unique and not-null on the key, accepted values on statuses, relationships to dimensions, freshness, and a row-count check against the source.
- "Finance says revenue is off by 2%. How do you find out why?" Reconcile per order between the source and each step of the pipeline until the step where the difference appears.
Go further
dbt's documentation on data tests describes the same four generic tests (unique, not null, accepted values, relationships) as queries that return failing rows.
Practise it
Write the Data Tests for a Fact Table is this lesson as a problem. Then Find Orphaned Orders Before They Reach the Dashboard, and Reconcile Payment Processor Payouts Against the Bank.