In two sentences
The grain of a table is what one row means: "one row per order line", "one row per customer per day". Fact tables hold measurements at a declared grain, dimension tables describe the things they're about, and most wrong numbers come from mixing two grains in one query.
See it
Two orders, one with three lines. The shipping fee is stored per order:
| order_id | shipping_fee |
|---|---|
| 1 | 5 |
| 2 | 5 |
| order_id | line_no | sku | amount |
|---|---|---|---|
| 1 | 1 | A | 20 |
| 1 | 2 | B | 10 |
| 1 | 3 | C | 10 |
| 2 | 1 | A | 20 |
Join them and sum: sales 60 ✓, shipping 20 ✗ (it's 10). Shipping lives at the order grain; the query works at the line grain, so order 1's fee is counted three times.
Two fixes: aggregate lines to the order grain before joining, or allocate the fee down to the lines so it's stored at the line grain:
| order_id | line_no | amount | shipping share |
|---|---|---|---|
| 1 | 1 | 20 | 2.50 |
| 1 | 2 | 10 | 1.25 |
| 1 | 3 | 10 | 1.25 |
| 2 | 1 | 20 | 5.00 |
In SQL
Allocate an order-level amount to lines, proportionally to each line's amount:
SELECT l.order_id, l.line_no, l.amount,
o.shipping_fee * l.amount / sum(l.amount) OVER (PARTITION BY l.order_id) AS shipping_share
FROM orders o
JOIN order_lines l ON l.order_id = o.order_id;The shares add up to the fee for every order, so sums at any level stay right.
Test the grain you declared: no row may share the key.
SELECT order_id, line_no, count(*)
FROM fct_order_lines
GROUP BY order_id, line_no
HAVING count(*) > 1; -- must return nothingWhen to use it
| fact table type | one row per | example |
|---|---|---|
| transaction | event | order line, payment, click |
| periodic snapshot | thing per period | account balance per day, stock per product per day |
| accumulating snapshot | process instance, updated as it moves | order with placed, paid, shipped, delivered dates |
Best practices
- Write the grain in one sentence before writing SQL, in the table's description. If you can't, the table isn't designed yet.
- Choose the lowest grain the source gives you. You can always aggregate up; you can never split down.
- Dimensions get surrogate keys (a meaningless integer per version), so type 2 history and several sources with clashing ids both work. Facts store that key.
- Keep an "unknown" member (key
-1) in every dimension, so a fact with a missing reference still joins and still counts. - Many-to-many (an order with several tags) goes through a bridge table with a weight per row, or every tag gets the full revenue.
Common mistakes
- Joining two facts directly. Orders × shipments × payments, each at its own grain, multiplies rows. Aggregate each to a shared grain first (often order), then join.
- A dimension that isn't unique on its key. One duplicate customer doubles every order of that customer. Test it.
- Summing a measure that belongs to a coarser grain (the shipping fee above, an order discount on lines). Allocate it, or keep it in its own table.
- Mixing grains in one table: order rows and line rows in the same fact, told apart by a flag. Every query then needs to remember the flag.
What an interviewer asks next
- "Design the tables for a ride-hailing app." Start with the grain of each fact: one row per trip, one per payment, one per driver per day. Then the dimensions: rider, driver, city, date.
- "Why surrogate keys, if the source has ids?" Versions (type 2), merged sources with overlapping ids, and stable keys when the source recycles them.
Go further
Kimball's "declare the grain" page is the rule this lesson is built on. The star schema page shows the overall shape.
Practise it
Revenue Doubled Overnight: Fix the Fan-Out Join, then Model a Ride-Hailing Star Schema from Operational Tables, then Revenue by Category Without Double Counting (Bridge Table).