Skip to content

Lesson 7 ยท Dimensional Modeling

Star and Snowflake Schemas

Build a star schema in SQL, compare it with a snowflake schema, learn about galaxy schemas and wide tables, and write the standard star-join query.

A star schema has a fact table in the middle and dimension tables around it. Each fact row holds foreign keys that point to one row in each dimension, plus numeric measurements. Drawn as a diagram, it looks a bit like a star.

It is popular because it is easy to understand and because query engines and BI tools generally handle it well.

The shape

flowchart TB
  D["dim_date"] --- F["fact_sales"]
  P["dim_product"] --- F
  S["dim_store"] --- F
  C["dim_customer"] --- F

Dimensions surround the fact table; each connects to it directly.

Fact table Dimension table
Holds Keys and measurements Descriptive attributes
Rows Many, keeps growing Far fewer, usually
Width Narrow Often wide
Used for Summing and counting Filtering, grouping and labels

Build one

SQL A small star schema and a star join

Press Run (or Ctrl + Enter) to see the result here.

To ask a new question, you usually change only the SELECT, WHERE and GROUP BY columns. Try grouping by s.city or p.brand.

SQLite does not check foreign keys unless you turn that on, and many analytical platforms declare them without enforcing them. Your loading process must therefore make sure every key in the fact table exists in its dimension.

Snowflake schemas

In a snowflake schema, a dimension is normalized into several tables. Instead of repeating the category name on every product row, dim_product holds a category_key that points to dim_category, which may point to dim_department.

SQL
-- Read-only: the snowflaked form of the product dimension
CREATE TABLE dim_category (
  category_key INTEGER PRIMARY KEY, category_name TEXT, department TEXT
);
CREATE TABLE dim_product_snow (
  product_key INTEGER PRIMARY KEY, product_name TEXT, brand TEXT,
  category_key INTEGER REFERENCES dim_category
);
Star Snowflake
Joins Fewer More
Repetition in dimensions More Less
Ease for readers Usually simpler Usually harder
Updating a category name Many dimension rows One row

Dimensions are normally small compared with fact tables, so the space saved by snowflaking is often minor. Choose the star by default. Snowflaking can be reasonable when a sub-table is maintained separately, is shared, or is very large. A middle path keeps normalized tables in the core and publishes a flattened dimension view for readers.

"Snowflake schema" is a modeling term. It has nothing to do with the Snowflake data platform, which can host any schema shape.

Galaxy schemas and wide tables

Real warehouses have many fact tables, such as sales, inventory and returns, sharing dimensions such as date and product. This arrangement is sometimes called a galaxy schema or fact constellation.

Some teams go further and publish one big table: the facts with dimension attributes already joined in. It is simple to query and can perform well on columnar engines. However, it repeats data, mixes history rules into one place and makes shared dimensions harder to reuse. A good approach is to keep the star as the model and generate wide tables from it when a tool needs them.

Naming tips

  • Prefix tables: fact_ and dim_ (or use separate schemas).
  • Use readable column names such as product_name, not prd_nm.
  • Store decoded values: 'Loyalty member' is clearer than 1.
  • Give keys one consistent suffix, such as _key.

For more practice, see star schemas in SQL.

Read a business question as a query

Most questions translate into a star join almost word for word. Take "Show footwear units by city for the first quarter."

Words in the question Part of the query
units SUM(f.quantity)
by city GROUP BY s.city
footwear WHERE p.category = 'Footwear'
first quarter AND d.quarter = 'Q1' AND d.year = 2026

Notice that the year was not in the question. In a table with several years, "first quarter" alone would silently add them together. Ask what the person means, and include year with any quarter or month label.

What dimensions without sales look like

The join starts from the fact table, so a store with no sales in the period does not appear in the result at all. When the question is "show every store", start from the dimension, use a LEFT JOIN to the facts and turn missing sums into zero with COALESCE. Put conditions on the facts in the join condition or in a subquery. A condition on a fact column in WHERE would remove the unmatched stores again.

Why not one table per report?

A tempting alternative is to build a separate summary table for every dashboard. It works for a while. Then a user asks to filter a chart by brand, and the brand was not included. Another report calculates revenue in a slightly different way. Soon you maintain many tables that nobody can compare.

A star at a detailed grain answers a wide range of questions from one place. When a particular report needs speed, derive a summary from the star, so that both share the same definitions.

How wide can a dimension be?

Dimensions with many columns are normal. A product dimension may hold brand, several category levels, size, color, material, supplier name and launch date. Extra columns cost little in the fact table, which stores only the key, and they save analysts from hunting for lookup tables. Do keep them meaningful: remove columns that nobody can explain, and avoid free-text dumps with sensitive content.

Common mistakes

Do not join two fact tables directly row to row, for example sales to returns on product. Each sale row can match many return rows, which multiplies the totals. Summarize each fact table separately to the same level first, then join the summaries. The next lesson shows how.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What does a fact table mainly contain?

    Facts measure events; keys connect each row to its context.
  2. 2. What distinguishes a snowflake schema from a star schema?

    Snowflaking splits dimensions, adding joins and reducing repetition.
  3. 3. Why is a star usually preferred for the layer analysts query?

    Readability and straightforward joins are the main benefits.

Summary

  • A star schema is a fact table surrounded by descriptive dimensions.
  • The typical query joins, filters and groups by dimension attributes, then aggregates facts.
  • Snowflake schemas normalize dimensions at the cost of more joins.
  • Galaxy schemas share dimensions among many fact tables; wide tables can be derived from stars.

Next: Conformed Dimensions and the Bus Matrix

Your progress is saved in this browser. No account needed.