Notes
ETL
ETL means extract, transform, load. For ShopRung, a nightly job could extract orders, order items and products, transform them by joining, converting cents to amounts, totalling sales per day and category, and dropping test accounts, then load the result into a warehouse table that a sales dashboard reads. Many modern teams do ELT instead: load raw data first, then transform inside the warehouse with tools like dbt.
- extract: orders, order_items, products from ShopRung
- transform: join, convert cents to amounts, daily totals by category, drop test users
- load: into a warehouse table daily_sales
- used by: the sales dashboard
- ELT: load first, transform inside the warehouse (dbt)
Mapping
The specification for data work is a source to target mapping. For each target column, it says where the value comes from and which rule transforms it: sale date is the date of the order's created at, units is the sum of item quantities, revenue is the sum of totals divided by a hundred to two decimals, and cancelled orders and staff accounts are filtered out. Testers derive test cases from every row, exactly like requirements.
target column source / rule
sale_date date(orders.created_at)
category products.category
units SUM(order_items.qty)
revenue SUM(orders.total)
/ 100, 2 decimals
filter status != CANCELLED
and role = CUSTOMER Reconciliation
Reconciliation compares source and target. Row counts: the number of qualifying orders must match what the target was built from. Sums: total revenue in the source, in cents, must equal the target's revenue times a hundred. When they differ, do not stop at the counts; list the keys that are missing or extra, because a count can match while different rows are wrong.
-- row counts SELECT COUNT(*) FROM orders WHERE status != 'CANCELLED'; -- vs rows feeding daily_sales -- money SELECT SUM(total_cents) FROM orders WHERE ...; -- = SUM(revenue) * 100 in target differences → list the missing or extra keys, not just a count
Invariants
Invariants are rules that must always be true. Each order's total must equal subtotal minus discount plus shipping. Each order's subtotal must equal the sum of its items' price times quantity. A good data check returns the rows that break the rule, so an empty result means pass. We placed three orders in ShopRung through its API and ran both: zero rows, zero rows. Pass.
-- total = subtotal - discount -- + shipping SELECT id FROM orders WHERE total_cents != subtotal_cents - discount_cents + shipping_cents; -- subtotal = sum of its items ...GROUP BY o.id HAVING o.subtotal_cents != SUM( i.price_cents * i.quantity); → after 3 orders: 0 rows, 0 rows
Stock reconciliation
Then a reconciliation across tables. The Clean Code Notebook started with a stock of forty. We ordered three, then four, so seven were sold and stock should be thirty three. The database said thirty seven. The second order had run with ShopRung's stock not reduced bug turned on. Every API call returned two hundred, and nothing looked wrong on screen; only the data showed the bug.
seed stock 40 ordered: 3 + 4 = 7 sold expected stock 40 - 7 = 33 actual stock 37 the second order ran with the stock-not-reduced bug on every API call returned 200; only the data shows the bug
Integrity
ShopRung's tables declare no foreign keys, so the database does not protect relationships itself. We inserted an order item pointing at product nine nine nine, which does not exist, and it was accepted. The orphan check, a left join where the product is missing, found it straight away, and the subtotal invariant now failed for order three too. Where constraints are missing, tests must cover them.
INSERT INTO order_items VALUES (3, 999, 'Ghost', 100, 1); → accepted: no FOREIGN KEY declared on order_items SELECT i.order_id, i.product_id FROM order_items i LEFT JOIN products p ON p.id = i.product_id WHERE p.id IS NULL; → (3, 999) and order 3's subtotal no longer matches
The full list
The full list, often called data quality dimensions. Completeness: required values present and all rows arrived. Uniqueness: no duplicate keys. Validity: correct types, ranges and allowed values. Consistency: invariants and totals agree. Integrity: no orphans. Accuracy: values match the source. Freshness: the data loaded on time. And idempotency: running the job twice must not double the numbers.
- completeness: required values present, all rows arrived
- uniqueness: no duplicate keys
- validity: types, ranges, allowed values
- consistency: invariants, totals
- integrity: no orphans
- accuracy: matches the source
- freshness: loaded on time
- idempotent: rerun = same result
Tools
Data checks belong in the pipeline as code. In dbt, a schema file declares tests per column: unique, not null, accepted values, and relationships, which is the orphan check. Great Expectations and Soda do the same with their own formats, and plain pytest with SQL queries works well too. Run them after every load, and stop bad data before it reaches dashboards.
# dbt schema.yml
columns:
- name: order_id
tests: [unique, not_null]
- name: product_id
tests:
- relationships:
to: ref('products')
field: id
# also: Great Expectations, Soda,
# pytest + SQL Common mistakes
Mistakes. Testing with ten hand made rows, when production has millions with odd values. Stopping when counts match. Not checking for duplicates after a job is rerun. Ignoring time zones, so sales slip into the wrong day. Rounding differences, because summing then rounding differs from rounding then summing. And copying real personal data into test systems instead of masking it.
- testing with 10 hand-made rows
- counts match, so "it's fine"
- no check for duplicates after a rerun
- time zones and dates ignored
- rounding: sum then round vs round then sum
- real personal data copied into test systems
Say it like this
In the interview: I start from the source to target mapping and test each column's rule. Then I reconcile row counts and sums with the source and list the missing or extra keys, and check uniqueness, nulls, ranges, orphans and business invariants, plus freshness and that a rerun gives the same result. These run as SQL or dbt tests after every load, with production like volumes and masked data.
- "From the source-to-target
- mapping: per column rules, then reconcile counts and sums with the source, list missing keys, check uniqueness, nulls, ranges, orphans and business invariants, freshness and rerun idempotency.
- As SQL or dbt tests after every load, with production-like
- volumes and masked data."