Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

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
SQL topics
Lesson 108/ 160
06. Database Design, Normalization & Semi-Structured SQL

Star Schema & Snowflake Schema Design for Analytics

Design fact and dimension tables in star or snowflake layouts for BI.

IntermediateFact · Dim · Star3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Keep the numbers in one big table, and the descriptions in smaller tables around it, joined by ids.

Think of it like…

A sales ledger (the fact table) where each line just records amounts and reference numbers, plus separate directories for customers, products and dates that explain what each reference number means.

Where you’ve seen it

An e-commerce warehouse: fact_order_lines (date_key, customer_key, product_key, quantity, revenue) joined to dim_date, dim_customer and dim_product. "Revenue by category and month" is two joins and a GROUP BY.

Words you’ll hear

Fact table
Measurable events at a declared grain, with numeric measures and foreign keys to dimensions.
Dimension table
Descriptive attributes for filtering and grouping (customer city, product category).
Grain
Exactly what one fact row represents, such as one product line on one order.
Surrogate key
A warehouse-generated id for dimension rows, which also supports keeping history (SCD).

Visual diagram

Loading interactive diagram

Theory & example queries

Facts, dimensions and grain

Design starts by declaring the grain: "one row per product per order". Every measure in the fact table must be true at that grain. Additive measures (revenue, quantity) can be summed across any dimension; semi-additive ones (account balance) can't be summed over time; ratios should be stored as their numerator and denominator.

Dimensions hold the descriptive attributes people filter and group by. A dedicated date dimension (with day of week, holiday flags and fiscal periods) makes calendar logic easy. Dimensions usually have surrogate keys, which also let you keep history when attributes change (slowly changing dimensions).

Revenue by category and monthQuery example
1Input dataStart from the source rows or state that the statement sees.
This statement works with the current schema, configuration, or database state rather than a row dataset. The command still follows the same input → query → result flow.
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1SELECT2  d.year_month,3  p.category,4  SUM(f.revenue)5FROM6  fact_order_lines f7  JOIN dim_date d ON d.date_key = f.date_key8  JOIN dim_product p ON p.product_key = f.product_key9GROUP BY10  1,11  2;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Project and calculate

Calculate the selected expressions and return the requested output columns.

1/11
3OutputInspect the representative rows, affected-row count, plan, or command status.
Monthly revenue by category3 sample rows
Monthly revenue by category
year_monthcategoryrevenue
2026-10Electronics425000
2026-10Home318500
2026-11Electronics467250

Note: Representative joined fact and dimension rows; revenue grouping and label format depend on the warehouse data.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Key takeaway: Declare the grain first; facts hold measures, dimensions hold descriptions.

Star or snowflake?

A star keeps each dimension denormalised: dim_product contains the category and department names directly. That repeats some text, but every query needs only one join per dimension, which BI tools and analysts handle easily.

A snowflake moves category and department into their own tables, which reduces duplication and makes some updates simpler, but adds joins to every query that uses them. Modern columnar warehouses (BigQuery, Snowflake, Redshift) compress repeated values well, so the storage savings matter less, and most teams default to stars, snowflaking only very large or shared hierarchies.

The same dimension, snowflaked

dim_product (product_key, name, category_key), dim_category (category_key, name, department_key), dim_department (department_key, name): "revenue by department" now needs three joins instead of one.

Key takeaway: Default to a star; snowflake only when duplication is a real problem.
Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Scope

Pick the business process

Choose one process to model, such as orders, shipments or page views. Each gets its own fact table.

Focus at this step

process = order lines

What to watch at this step
One fact table
per process
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

A star schema organises analytics data into a central fact table of measurable events (one row per order line, with amounts and quantities) surrounded by dimension tables that describe them (date, customer, product, store). A snowflake schema normalises those dimensions further, for example splitting product into product → category → department.

Why it matters: Star schemas are the standard model for data warehouses and BI tools because they make reporting queries simple and fast: one join from the fact to each dimension. Data engineering and analytics interviews regularly ask you to design one, starting with declaring the fact table's grain.

When to reach for it

  • Designing a data warehouse or analytics mart
  • Feeding BI tools such as Power BI, Looker or Metabase
  • Data engineering design interviews

When to avoid or be careful

  • Mixing grains in one fact table (order totals and order lines together)
  • Putting descriptive text in fact tables
  • Snowflaking every dimension by default

Star vs snowflake vs one big table

Star schema
Best for: Most BI and reporting workloads

Strengths: Simple queries, one join per dimension, BI-tool friendly.

Watch out: Repeated attribute values in dimensions.

Snowflake schema
Best for: Large or shared hierarchies that change often

Strengths: Less duplication; hierarchies managed in one place.

Watch out: More joins and more complex queries.

One wide denormalised table
Best for: A single heavily used dataset in a columnar warehouse

Strengths: No joins at all for consumers.

Watch out: Hard to maintain, and history and attribute changes get messy.

Interview signals & numbers to know

When this topic comes up

  • “Design a data warehouse for …”
  • “Fact vs dimension tables”
  • “Star vs snowflake schema”
  • “What's the grain?”
  • BI reporting model

Benchmarks & rules of thumb

1 -
grain per fact table
1 join -
per dimension in a star
3 types -
of measures: additive, semi-additive, non-additive

Common mistakes & follow-ups

Common interview mistakes

  • Not declaring the grain, then summing order-level shipping fees on order-line rows.
  • Storing a ratio such as conversion rate in the fact table, so it gets summed or averaged incorrectly.
  • Joining facts to dimensions on natural keys whose values change over time.

Likely interviewer follow-ups

  • How would you design a star schema for a food-delivery app's orders?
  • What are slowly changing dimensions, and how would you store a customer moving city?
  • When would you choose a snowflake over a star?
Interview tip: In a design question, say the grain out loud first ("one row per item per order"), then list dimensions and measures. Interviewers look for the grain before anything else.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Star Schema & Snowflake Schema Design for Analytics with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Strategic Denormalization for Read-Heavy Analytical Workloads
Next topic
Slowly Changing Dimensions (SCD Type 1 & Type 2) in SQL