Star Schema: Facts and Dimensions

One narrow table of things that happened, surrounded by wide tables describing them. The shape warehouses are built in.

Star Schema: Facts and Dimensions

Same shape, different slices

query.sql SQLite
Result

Worth knowing

The fact table holds measurements — one row per event, mostly numbers and foreign keys. It is long and narrow.
Dimension tables describe the context — dates, products, stores. Short and wide, full of text.
Every query is the same shape: join the fact table to whichever dimensions the question mentions, group, aggregate.
It is deliberately denormalised. A snowflake schema normalises the dimensions and trades query simplicity for tidiness.

Overview

A fact table records events or measurements, such as one order line, one payment, or one daily inventory snapshot. Its grain is the precise meaning of one row. A dimension table describes entities used to filter and group facts, such as customer, product, store, or date.

In this guide
  1. Define the grain first
  2. Why the grain prevents wrong totals
  3. Dimensions can change over time
  4. State the grain before adding dimensions
  5. Use conformed dimensions carefully
  6. Further reading

Define the grain first

A fact table can contain foreign keys to dimensions and numeric measures. Dimension tables are denormalized for convenient analysis. The layout resembles a star because dimensions connect to the central fact table.

Why the grain prevents wrong totals

Suppose one fact row represents an order line. If a query joins it to a dimension with multiple matching rows, the line can be duplicated and the sum inflated. Before aggregating, verify that each fact key matches at most one dimension row for the relevant point in time.

Choose measures that can be aggregated as intended. Revenue can often be summed; a precomputed average generally cannot be summed or averaged correctly without its count or denominator. Ratios should usually be recomputed from additive components.

Dimensions can change over time

A customer may move to a new region. A slowly changing dimension strategy determines whether historical facts use the old description, the new one, or both through versioned dimension rows. The choice must match the reporting question. A date dimension can also store calendar, fiscal, and holiday attributes that are awkward to derive repeatedly.

Star schemas are common in warehouses, but their details depend on the warehouse engine and modeling needs. See OLTP vs OLAP and normalization.

State the grain before adding dimensions

Suppose one fact row is one product line on one order. Joining a line to a product dimension should match one product version for the order's relevant date. If the dimension contains multiple historical versions and the join omits the effective date, the line may match multiple rows and revenue is doubled. Add a test that counts matched dimension rows per fact key. When totals change after adding a dimension, inspect join cardinality before assuming the aggregation is wrong.

Use conformed dimensions carefully

A shared date or customer dimension can make reports across fact tables consistent, but only if its keys and definitions mean the same thing in each model. Joining two fact tables through dimensions can still multiply rows when both have many records per dimension key. Aggregate each fact to a compatible grain before combining measures. Document which dimensions are shared and which attributes are historical. Consistent naming does not guarantee compatible business definitions; the warehouse model should make those definitions explicit.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does a fact table contain?

  2. Why is mixing grain in a fact table so dangerous?

  3. Why do star schemas usually beat snowflake schemas in a warehouse?

Cheat sheet

Star Schema: Facts and Dimensions

Build a star schema with one fact table and three dimensions, run an analytical query, and see why columnar engines prefer this layout.

DATABASE · vizlearn.in/database/star_schema.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.