Why defining 'what one row represents' (Grain) is the #1 rule in analytics—how grain drift causes duplicate KPI inflation, why `SELECT DISTINCT` hides bugs, and how to prove grain in SQL.
Suppose a delivery truck drops off 10 crates, and each crate holds 12 apples.
- If someone hands you a spreadsheet without telling you what one row means—is 1 row = 1 crate or 1 row = 1 apple?
- If half your rows represent crates (weight = 20 lbs) and half represent loose apples (weight = 0.4 lbs), clicking `SUM()` produces complete nonsense!
In Data Engineering, the exact real-world meaning of one single row in a table is called its Grain (or granularity). Before writing a single CREATE TABLE or SELECT, an expert always writes down a 1-sentence Grain Statement:
- *"1 row in fact_orders = 1 unique customer checkout order."*
- *"1 row in fact_order_items = 1 individual product line inside an order."*
- *"1 row in fact_daily_inventory = 1 SKU at 1 Warehouse on 1 Calendar Day."*
When downstream analysts read gold.fact_subscriptions, they implicitly trust a mathematical promise: one row per `(subscription_id)` (or per (subscription_id, billing_month)).
When an upstream pipeline bug loads two ingestion batches without deduplication—suddenly putting 2 rows for subscription_id = 'SUB-901'—every downstream KPI breaks instantly:
- COUNT(*) doubles active subscriber counts!
- SUM(monthly_fee) doubles recurring revenue (MRR)!
- JOIN queries clone downstream rows silently!
Suppose `staging_payments` expects Grain = 1 row per `payment_id`, yet a retry webhook inserted a duplicate:
| payment_id | order_id | amount_usd | gateway_event_ts | status |
|---|---|---|---|---|
| PAY-101 | ORD-55 | 120.00 | 2026-05-01 09:00:00 | CAPTURED |
| PAY-101 | ORD-55 | 120.00 | 2026-05-01 09:00:04 | CAPTURED |
| PAY-102 | ORD-56 | 85.00 | 2026-05-01 09:12:00 | CAPTURED |
-- Grain Contract Check: 1 row per payment_id
SELECT
payment_id,
COUNT(*) AS occurrences
FROM staging_payments
GROUP BY payment_id
HAVING COUNT(*) > 1;Use deterministic `ROW_NUMBER()` ordered by latest timestamp:
WITH clean_payments AS (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY payment_id
ORDER BY gateway_event_ts DESC
) AS rn
FROM staging_payments
)
SELECT payment_id, order_id, amount_usd, gateway_event_ts, status
FROM clean_payments
WHERE rn = 1;| Approach | What Happens When Upstream Sends Retries? | Verdict |
|---|---|---|
| Blind `INSERT INTO` | Silent duplicate rows accumulate indefinitely. | Broken |
SELECT DISTINCT * | Still keeps duplicates if timestamps or batch IDs differ; burns RAM sorting all columns. | Anti-Pattern |
| `COUNT(DISTINCT order_id)` everywhere | Masks duplicates on counts while leaving SUM(amount) doubled! | Dangerous Band-Aid |
| Deterministic `ROW_NUMBER()` + `MERGE` / Grain Test | Enforces exact grain before writing Gold tables; fails loudly if duplicates slip past. | Production Standard |
order_shipping_fee on every order_item row breaks SUM(order_shipping_fee)! Keep order-level facts in fact_orders (Grain: order_id) and line facts in fact_order_items (Grain: (order_id, line_id)).order_items table's grain isn't just order_id AND isn't just product_id (what if a customer edits quantity on two separate promo lines?)—verify the true composite tuple (order_id, line_number).GROUP BY <grain_key> HAVING COUNT(*) > 1**.Test your understanding of the concepts covered in this lesson.
Global DynaDatum Bank stores account balances at the end of every month for thousands of customers. A junior engineer previously saved these balances as plain text files with no rules, which caused balance numbers to get corrupted and queries to be painfully slow. We need an official Delta Lake table that stores balance dates, money amounts, and currencies accurately. We also want queries to run fast by organizing the files by date, and prevent duplicate balance records for the same account on the same day.