Star Schemas
That question needs 4 joins across 5 tables (order_lines, orders, products, subcategories, categories) to produce 2 grouping columns and one sum. A different question tomorrow needs a different chain of joins. Analytical work asks hundreds of these questions, so the warehouse, the copy of the data kept for analysis, is reshaped around the questions.
One fact table, many dimensions
One row of fact_order_line means one product on one order. One row of dim_product means one product, and that row also carries the product's subcategory and category. The categories and subcategories tables are gone, folded into the product they describe.
That folding is the strategic denormalization you already know, applied on purpose. The category name 'Shelter' repeats on every tent and tarp row, and the redundancy is cheap: a dimension has thousands of rows, is loaded by one pipeline, and is read far more than it is written. The order_number column stays on the fact row with no table of its own, a choice the dimensions section returns to.
Every question has the same shape
Each dimension is one join away from the fact table. Swap p.category for c.loyalty_tier, or d.year_month for d.day_of_week, and the query keeps its skeleton. You join the dimensions you need and filter on their attributes, then group and sum the facts. The join path never has to be worked out again for a new question.
Why the shape is fast
The 2 kinds of table have opposite proportions. A fact table is long and narrow: hundreds of millions of rows, each a handful of integer keys and numbers. A dimension is short and wide: dim_product might hold 40,000 rows and 30 text columns. A filter such as category = 'Shelter' can run against the 40,000-row table first and yield a small set of product keys. The engine then scans the fact table comparing integers, never long strings.
- Measurements of an event
- Many rows, few columns
- Foreign keys plus numbers
- Grows with every event
- Descriptions of a thing
- Few rows, many columns
- One key plus text attributes
- Grows when a new product or customer appears
An analyst at Fernhill wants units sold by customer loyalty tier and day of week. What does the star above need before this query can run?
Transaction: one row per event
A transaction fact table records an event at the moment it happens. fact_order_line is one: a customer buys 2 headlamps, and a row lands with the date, the product, the customer, a quantity of 2 and an amount of 59.90. The row is written once and never touched again. If nothing sells, nothing is written, so the table is sparse: a product with no sales on Tuesday has no Tuesday row.
Periodic snapshot: one row per period
A periodic snapshot fact table measures a level at a fixed interval. Every night, fact_inventory_daily gets one row for every product in every warehouse, holding the quantity on hand at close of day. The table is dense: a tent that nobody touched still gets its row, saying 14 again.
| date_key | product | quantity |
|---|---|---|
| 20260302 | Ridge 2P Tent | 1 |
| 20260304 | Ridge 2P Tent | 2 |
| date_key | product | quantity_on_hand |
|---|---|---|
| 20260302 | Ridge 2P Tent | 14 |
| 20260303 | Ridge 2P Tent | 14 |
| 20260304 | Ridge 2P Tent | 12 |
On 3 March (date_key 20260303) the left table says nothing and the right table says 14. That row for a day without events is the whole point of a snapshot. The price is volume: 40,000 products in 6 warehouses come to 240,000 new rows each night, even on a night with no sales. A snapshot row is also a level, so read one date at a time: adding the 14 from 2 March to the 14 from 3 March counts the same tents twice.
Accumulating snapshot: one row per process
| As of | placed_date_key | shipped_date_key | delivered_date_key | days_to_ship | days_to_deliver |
|---|---|---|---|---|---|
| 2 March | 20260302 | not yet | not yet | not yet | not yet |
| 4 March | 20260302 | 20260304 | not yet | 2 | not yet |
| 7 March | 20260302 | 20260304 | 20260307 | 2 | 5 |
All 3 lines of that table are the same row of fact_order_fulfillment, read on 3 different days. Because every milestone sits on one row, the time between them is a subtraction. The table stores the results as days_to_ship and days_to_deliver. Average delivery time by warehouse becomes one AVG over one column. Building the same answer from a transaction table means finding each order's 'shipped' event and its 'delivered' event and pairing them up.
Choosing between the 3
| Property | Transaction | Periodic snapshot | Accumulating snapshot |
|---|---|---|---|
| One row is | One event | One entity in one period | One run of a process |
| Rows appear | When an event happens | On a schedule, event or not | When the process starts |
| Rows change | Never | Never | At every milestone |
| Answers | How much happened? | What was the level? | How long did each step take? |
| Fernhill table | fact_order_line | fact_inventory_daily | fact_order_fulfillment |
Fernhill's finance team wants the outstanding gift card balance for every card at the end of each month, including cards nobody used that month. Which fact table fits?
A fact row for order 5001 says: date 20260302, product 318, customer 7741, quantity 1, amount 249.00. Those numbers mean nothing until a dimension translates them. Dimensions are where the words live, and the words are what analysts type into WHERE and GROUP BY.
Anatomy of a dimension row
| product_key | sku | product_name | subcategory | category | brand |
|---|---|---|---|---|---|
| 318 | TNT-204 | Ridge 2P Tent | Backpacking Tents | Shelter | Fernhill |
| 319 | TNT-310 | Basecamp 6P Tent | Family Tents | Shelter | Fernhill |
| 502 | LGT-011 | Beam 400 Headlamp | Headlamps | Lighting | Lumora |
More columns make a better dimension. Each attribute added to dim_product is a new way to slice every fact table that points at it, at the cost of one column on a 40,000-row table.
The date dimension
Almost every star has a date dimension: one row per calendar day, built once for decades ahead. A DATE column can already give you the month, but it cannot tell you the fiscal quarter, whether the day was a public holiday, or which week of Fernhill's spring sale it fell in. dim_date stores those as plain columns, so 'sales on holidays' is a filter instead of a formula every analyst writes differently.
Conformed: one dimension, many stars
Fernhill has 2 fact tables that mention products: fact_order_line and fact_inventory_daily. A conformed dimension is one that both use with the same keys and the same attribute values. Because 'Shelter' means the same set of products in both stars, units sold by category and units on hand by category can sit in adjacent columns of one report.
Role-playing: one dimension, many jobs
fact_order_fulfillment carries a date key for each of its 3 milestones, from placed through delivered, and all 3 point at the same dim_date. Each join gives the table a different alias, and the alias is the role it plays in that query.
Orders that haven't shipped yet have no shipped date. If shipped_date_key is NULL on those rows, the inner join to shipped drops them from every count. Stars avoid this by pointing an empty milestone at a reserved dim_date row labeled 'Not yet', so unshipped orders stay in the result as their own group.
Degenerate: a key with no table
order_number 5001 is worth keeping: it groups the lines of one order and lets support look an order up. But the date has its own dimension, and so do the customer and the product, which leaves nothing to describe the order itself. A dimension table for it would hold one column. So the value stays in the fact table as a degenerate dimension: a dimension attribute with no dimension to join to.
Junk: a home for stray flags
| order_profile_key | is_gift_wrapped | channel | payment_type |
|---|---|---|---|
| 1 | No | Web | Card |
| 2 | No | Web | Wallet |
| 3 | No | App | Card |
| 4 | Yes | Web | Card |
Fernhill starts recording a returns fact table. Each return row needs the product returned and the day it came back. Leadership wants return rate by category beside sales by category. What should the returns star use for products?
Order 5001 at Fernhill has 3 lines and one shipping fee of 6.00. A report on shipping income shows 18.00 for that order. Every stored value is the one the source sent, and the query is an ordinary SUM. The error was designed in when the table was built without anyone deciding what a single row stands for.
Grain is a sentence
The grain of a fact table is the meaning of exactly one row, stated in business words. For fact_order_line the sentence is: one row per product per order. The sentence is written before any column is chosen, because it decides both remaining design steps.
- Which dimensions fit. A dimension belongs only if it has a single value for each row. One order line has one product, one customer and one order date.
- Which facts fit. A number belongs only if it is measured once per row. Quantity and line amount are. A fee charged once per order is not.
- What the key is. The grain's dimensions form the composite key. For order lines that is order_number plus product_key, unique because Fernhill's cart merges a repeated product into one line with a higher quantity.
A fact at the wrong grain
| order_number | product | quantity | net_amount | shipping_fee |
|---|---|---|---|---|
| 5001 | Ridge 2P Tent | 1 | 249.00 | 6.00 |
| 5001 | Beam 400 Headlamp | 2 | 59.90 | 6.00 |
| 5001 | Trail Stove | 1 | 45.00 | 6.00 |
SUM(net_amount) returns 353.90, which is right, because each amount belongs to its line. SUM(shipping_fee) returns 18.00, 3 times the truth, because the fee belongs to the order and was copied to each line. Every query that sums the column inherits the error, and the size of the error depends on how many lines each order has, so no constant corrects it.
Choose the finest grain the source offers
A coarser grain is tempting because it's smaller. Fernhill could store one row per product per day: if the average product sells on 9 orders a day, its 9 order-line rows collapse into 1 and the table shrinks about 9x. But detail thrown away at load time can't be recovered by a query. Order lines add up to daily totals with a GROUP BY. Daily totals can never be split back into orders, so 'what is the average order value' becomes unanswerable.
Fernhill's first sales table was built for one dashboard and holds daily totals per product. 3 new requirements are about to test what one row of it can express. Pick how the model should change at each stage.
| date_keyINTEGERFK | product_keyINTEGERFK | units_soldINTEGER | net_amountDECIMAL(12,2) |
|---|
Marketing wants to know which products are bought in the same order, to design bundles. What do you do with the table?
The grain sentence is the contract of a fact table. Anyone who reads it knows what a COUNT(*) counts and which sums are safe. When a new requirement arrives, the first check is whether it fits the sentence, and if it does not, it gets a table with a sentence of its own.
Fernhill retires the Ridge 2P Tent and, a year later, reuses its SKU TNT-204 for a new ultralight model. If fact_order_line pointed at products by SKU, 3 years of old tent sales would now join to the new tent's name and weight and land in its category. The fact table is correct and every report built on it is wrong.
A key the warehouse owns
| customer_key | source_system | source_customer_id | customer_name | city |
|---|---|---|---|---|
| 0 | none | none | Unknown customer | Unknown |
| 7741 | web | 1001 | Ana Ruiz | Denver |
| 7742 | retail | 1001 | Tom Becker | Leeds |
What the integer protects against
- Reused identifiers. The source hands TNT-204 to a new product. When the Ridge tent was retired, its row 318 was marked inactive, and the lookup matches only active rows. The SKU finds no match, so the new tent gets key 9020. Old sales keep key 318, because fact rows are never re-keyed.
- Colliding identifiers. The web store and the retail system both have a customer 1001. Keys 7741 and 7742 keep Ana and Tom apart.
- Reformatted identifiers. A migration turns customer id 1001 into C-0001001. One attribute on one dimension row changes, and no fact row is touched.
- Missing identifiers. A guest checkout has no customer id. The fact row points at key 0, the 'Unknown customer' row, so the foreign key is never NULL.
Without the unknown row, a NULL customer key would drop guest orders from any inner join to dim_customer, and revenue by city would quietly come up short. With key 0, those orders land in a visible 'Unknown' group and the total still matches.
Dimensions load before facts
The dimension loads first. For each incoming customer, the load looks up the pair of source_system and source_customer_id. A match reuses the existing key, and a new pair gets the next integer. The fact load then swaps every business key for its surrogate through that same lookup.
Dimensions load first so the fact load can look up every surrogate key.
The LEFT JOIN keeps every staged line, and COALESCE sends any line without a match to key 0. Row counts in and out stay equal as long as each pair appears once in the dimension, which is the check to run after every load.
Fernhill buys a rival store and loads its orders. The rival's SKU LGT-011 is a lantern, while Fernhill's LGT-011 is the Beam 400 Headlamp. dim_product matches incoming rows on SKU alone. What happens to the rival's lantern sales?
> A food delivery marketplace is building its first analytics model. Restaurants, couriers and customers each live in a separate operational system, and leadership wants order revenue, delivery times and courier availability on one dashboard.
Measurements in one central fact table, descriptions around it, and every analytical question takes the same shape.
- Category
- Data Modeling
- Difficulty
- intermediate
- Duration
- 30 minutes
- Challenges
- 12 hands-on challenges
Topics covered: The Star Schema, Types of Fact Tables, Types of Dimensions, Defining the Grain, Surrogate Keys
Lesson Sections
- The Star Schema (concepts: Star Schema, Snowflake Schema)
Fernhill Outdoor sells camping gear online. Its order system is in 3NF, which keeps writes safe: each fact about a product or a customer lives in exactly one place. The cost shows up when an analyst asks for revenue by product category by month. One fact table, many dimensions A star schema sorts every column into one of 2 kinds of table. A fact table holds measurements of a business event: quantities and amounts you can add up. A dimension table holds the descriptions you filter and group by: t
- Types of Fact Tables (concepts: Fact Tables, Metric Additivity)
Fernhill's operations team asks 3 questions in one week. How much did we sell yesterday? How much stock is in each warehouse right now? How long does an order take to reach the customer? Each one measures the business differently, and each needs a fact table with a different rhythm of rows. Transaction: one row per event That sparseness is why a transaction table struggles with the stock question. Stock on hand adds up every receipt since the warehouse opened and subtracts the sales, with return
- Types of Dimensions (concepts: Dimension Tables)
Anatomy of a dimension row A dimension has one row per thing being described and one column per way of describing it. Hierarchies are flattened onto the row: a tent's subcategory and category sit beside its name. Codes are spelled out: the row says 'Shelter', never category 3, because the report will print whatever the column holds. The date dimension Conformed: one dimension, many stars Without a conformed dimension, the sales team might build its own product table calling tarps 'Shelter' while
- Defining the Grain (concepts: Grain Definition)
Grain is a sentence A fact at the wrong grain Here is order 5001 with the shipping fee stored on every line. There are 2 honest repairs. Allocate the fee down to the grain, giving each line a share in proportion to its amount (4.22, 1.02 and 0.76), so the shares sum back to 6.00. Or keep the fee in a second fact table whose grain is one row per order. Both restate the fact at a grain where it's true once per row. Choose the finest grain the source offers
- Surrogate Keys (concepts: Surrogate Keys, Key Generation)
A key the warehouse owns You met surrogate keys as one way to generate identity. In a star they are the rule: every dimension row gets an integer assigned by the warehouse's own load, and fact tables store only that integer. The source system's identifier, the business key, is kept on the dimension row as an ordinary attribute. It is used to match incoming data and never used to join. What the integer protects against Dimensions load before facts The date dimension is the usual exception. Its ke