Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 37/ 160
02. SQL & Data Engineering Basics

Star & Snowflake Schema Modeling

Model facts and dimensions with star/snowflake designs for BI and warehouse performance.

AdvancedFacts · Dimensions3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

A big table of events sits in the middle, with smaller lookup tables around it that describe the who, what, when and where.

Think of it like…

A receipt (the fact) lists what was bought, when and where, with codes that point to reference books (the dimensions): the product catalogue, the store directory, the calendar. Reports are built by looking up the codes in the books.

Where you’ve seen it

A retailer's fact_sales has one row per order line (date_key, customer_key, product_key, store_key, quantity, net_amount). dim_customer is an SCD type 2 table: when a customer moves from segment 'silver' to 'gold' a new row with a new surrogate key and validity dates is added. To train a model on 2025 orders, the data scientist joins fact_sales to dim_customer through the order's customer_key, which already points to the version valid on the order date, so segment reflects what was known at the time.

Words you’ll hear

Fact table
Rows of measurable events at a declared grain, with foreign keys to dimensions.
Dimension table
Descriptive attributes (product, customer, date, store) for filtering and grouping.
Surrogate key
A warehouse-generated integer key for a dimension row, independent of source ids.
SCD type 2
Keeping history by adding a new dimension row with validity dates for each change.

Visual diagram

Loading interactive diagram

Theory & deep dive

Facts, dimensions and grain

Start with the business process and its grain: "one row per order line" or "one row per customer per day". The fact table holds foreign keys to dimensions and numeric measures that make sense at that grain. Additive measures (quantity, amount) can be summed across all dimensions; semi-additive ones (account balance, inventory level) can be summed across customers but not across time; non-additive ones (ratios, prices) must be recomputed from additive parts (sum of revenue divided by sum of units, not the average of per-row prices).

Dimensions hold attributes used in WHERE and GROUP BY: product category, customer city, day of week, holiday flag. A date dimension with one row per day (and columns for month, quarter, fiscal period, is_weekend) removes date arithmetic from every query. Degenerate dimensions such as order_number live in the fact table, and factless facts (a student attended a class) record events with no measures.

A typical star query

SELECT d.year_month, p.category, SUM(f.net_amount) AS revenue FROM fact_sales f JOIN dim_date d ON d.date_key = f.date_key JOIN dim_product p ON p.product_key = f.product_key WHERE d.year = 2026 GROUP BY 1, 2; Each join is many-to-one, so the sum isn't inflated.

Key takeaway: Declare the grain, keep measures additive where possible, and join facts to dimensions many-to-one.

Star vs snowflake, history and ML features

A star denormalises each dimension into one wide table (product with category and department columns): fewer joins, simpler queries, faster scans, and BI tools love it. A snowflake normalises dimensions (product → category → department) to reduce duplication and ease updates, at the cost of extra joins. Modern columnar warehouses compress repeated values well, so stars are the usual choice; snowflaking is used when a hierarchy is large or shared.

History: with SCD type 1 you overwrite changes (no history); with type 2 you add a row per version, so facts keep pointing to the version current at the event time. For features, decide whether you want the attribute as of the event (type 2 key from the fact) or as of today (join through the natural key to the current row), and don't mix them accidentally. When comparing two business processes (sales and returns) don't join facts directly; aggregate each to shared (conformed) dimensions such as product and month, then join the summaries. For one row per customer, aggregate the fact to customer level first, then attach customer attributes.

Customer-level features from a star

SELECT c.customer_key, COUNT(DISTINCT f.order_id) AS orders_90d, SUM(f.net_amount) AS spend_90d FROM fact_sales f JOIN dim_customer c USING (customer_key) JOIN dim_date d USING (date_key) WHERE d.date >= :snap - 90 AND d.date < :snap GROUP BY 1; Features come only from before the snapshot.

Key takeaway: Stars for simplicity, SCD 2 for history, conformed dimensions to compare facts.

How it works, step by step

Step 1 of 4
Model

1. Pick the process and grain

Choose the business process (sales) and declare the grain (one row per order line).

fact_sales: 1 row per order line
Key numbers at this step
Grain
before columns

When to use it & trade-offs

What it is

Dimensional modelling organises warehouse data into fact tables (events or measurements with numeric measures, at a declared grain) surrounded by dimension tables (descriptive attributes used to filter and group). In a star schema each dimension is a single denormalised table joined directly to the fact; in a snowflake schema dimensions are normalised into sub-dimensions (product → category → department). Dimensions get surrogate keys, and changing attributes are tracked with slowly changing dimensions (SCD type 2). Analysts and ML engineers join facts to dimensions to build features and reports.

Why it matters: A well-modelled warehouse makes questions easy and correct: revenue by category by month is one fact and two dimension joins. A poor one forces five-way joins through tables of different grains and invites double counting. For data scientists the key skills are to identify the grain, to join facts to dimensions without fan-out, to handle history with SCDs for point-in-time features, and to avoid joining two fact tables directly.

When to reach for it

  • Revenue, funnel and engagement reporting
  • Building point-in-time features from facts and dimensions
  • Comparing processes through conformed dimensions

When to avoid or be careful

  • Joining two fact tables directly
  • Summing semi-additive measures across time
  • Mixing event-time and current attributes by accident

Star vs snowflake vs wide table

Star schema
Best for: BI, reporting and feature building

Strengths: Few joins; simple, fast queries.

Watch out: Some duplicated attribute values in dimensions.

Snowflake schema
Best for: Large shared hierarchies and strict normalisation

Strengths: Less duplication; easier attribute updates.

Watch out: More joins and complexity.

One wide table
Best for: A single narrow use case or ML training set

Strengths: Simplest consumption.

Watch out: Duplication, mixed grains and rigid structure.

Interview signals & numbers to know

When this topic comes up

  • “Design a warehouse schema for sales”
  • “Star vs snowflake”
  • “Track customer segment changes over time”
  • SCD type 2
  • Conformed dimensions

Benchmarks & rules of thumb

1 -
declared grain per fact table
many-to-one -
every fact-to-dimension join
2 -
common SCD types: overwrite (1) and add a row (2)

Common mistakes & follow-ups

Common interview mistakes

  • Joining sales and returns facts on product and date, multiplying rows.
  • Averaging unit prices across rows instead of dividing total revenue by total units.
  • Joining today's customer row to historical facts when the model needs the segment at the time.

Likely interviewer follow-ups

  • What is the grain of a fact table and why does it matter?
  • How do you track a customer's segment history?
  • When would you choose a snowflake over a star?
Interview tip: Open with "the grain is…". Every dimensional-modelling answer should start with that sentence.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Star & Snowflake Schema Modeling with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
OLTP vs OLAP & Data Warehouses
Next topic
ETL vs ELT Pipelines