shuffleet
Home
Practice
Problems
Lessons
Plans
Compete
Challenges
Interviews
Leaderboard
Progress
Skills
Analytics
Submissions
Account
Settings

Data tests and reconciliation

Junior · 4 min read

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:

testfailing rows
customers.customer_id is unique1 (c2 appears twice)
customers.email is not null1
orders.customer_id exists in customers1 (c9)
orders.status is one of the accepted values1 (lost)
orders loaded in the last 24 hours0

And a reconciliation between the payment provider (PSP) and the ledger:

payment_idresult
p1ok
p2amount differs (50.00 vs 49.50)
p3missing in ledger
p4missing at PSP

Each test is a query returning failing rows; a full outer join lines up two systems and labels each difference

In SQL

The five tests, as one results table:

SQL
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.

SQL
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

Tests at three places in a pipeline: on landing, on the cleaned tables, and before publishing

wherewhat to testif it fails
right after landing raw datarow counts against the source, schema, freshnessalert; don't build on top
on cleaned tablesunique keys, not-null keys, accepted values, referential integritystop the pipeline
before publishing (write-audit-publish)business rules and totals against the source of truthdon't publish; quarantine
on reports people usereconciliation with finance or the source systemflag 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 IN in an accepted-values test. status NOT IN (…) is unknown for a NULL status, so NULLs pass. Test status 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.