Learn SQL
-- advanced.report_reconciliation_except

Scenario: Reconciling Reports in SQL

Freeze, diff both ways, codify definitions

--concept

Revenue differs between reports: freeze definitions (tables, filters, NULL handling, time zone, dedup rule), reproduce side by side, diff row sets (EXCEPT both directions), trace to divergent predicate — usually fan-out double-counting, timezone day boundaries, or NULL inclusion. Metric definitions belong in versioned code (dbt/metrics layer), not tribal knowledge.

--how it works
1

Freeze definitions

Tables, filters, zones, NULL rules, dedup keys — written, same both sides.

2

Reproduce side by side

Same instant, same params — no moving windows between runs.

3

EXCEPT both ways

A−B plus B−A isolates each side’s extras for tracing.

4

Codify metrics

Versioned dbt/metrics definitions — tribal knowledge ends.

--visualization
(A EXCEPT B) + (B EXCEPT A) — each side extras isolated
sideextras
a-only3
b-only1
Stage 1 / 1
Trace fan-out → zones → NULLs, then codify metrics in versioned code.
--example
query.sql
1-- isolate extras both ways:
2(SELECT * FROM report_a EXCEPT SELECT * FROM report_b)
3UNION ALL
4(SELECT * FROM report_b EXCEPT SELECT * FROM report_a);
Result
divergencerank
fan-outusual suspect 1
timezonesuspect 2
NULL rulesuspect 3

Bidirectional EXCEPT surfaces rows unique to each report. Fan-out duplicates inflate one side; midnight-boundary zones shift the other; NULL inclusion widens a third.

--variants

Bidirectional diff

Each side’s extras surfaced.

query.sql
1(SELECT * FROM report_a EXCEPT SELECT * FROM report_b)
2UNION ALL
3(SELECT * FROM report_b EXCEPT SELECT * FROM report_a);
Result
sideextras
a-only3
b-only1
1One-way diff hides the other side.
2Counts direct the trace (fan-out inflates one).
3Row samples name the divergent predicate.

Usual-trio trace order

Fan-out, zones, NULLs — in that order.

query.sql
1-- 1. fan-out double-count? 2. midnight zones? 3. NULL inclusion?
Result
suspectcheck
fan-outcheck aggregations post-join
zonescheck day bounds
nullscheck inclusion rules
1Post-join SUMs double first.
2Day bounds shift second.
3NULL widens third.
--key facts
EXCEPT both waysisolates each side’s extras
Usual triofan-out, zones, NULLs
Codifymetrics in versioned code
--use cases
Finance reconciliations with signed counts
Migration parity: old vs new pipeline diffs
--under the hood
EXCEPT is set-op with dedup sorts — bound the diff with filters; UNION ALL preserves side labels.
Metrics layers compile one definition to both reports — divergence becomes impossible by construction.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.