Database Star and Snowflake Schemas
What you will learn
- What a star schema and a snowflake schema are
- How they differ and when each is used
- Roll-up and drill-down
- Slice and dice
- Why warehouses accept some repeated data
1. The star schema
In a star schema the fact table sits in the middle and each dimension is one flat table around it. Drawn out, it looks like a star:
dim_date
|
dim_store ── fact_sales ── dim_product
"Flat" means a dimension may repeat data. In dim_product below, the category name "Grocery" is stored on every grocery product row. That breaks the normalization rules of chapter 5 on purpose (lesson 5.7): dimensions are small, rarely written to, and a query needs only one join per dimension. Fewer joins make reports simpler and faster.
This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). It uses the same small sales warehouse as lesson 13.2 (six products, three stores, 181 days). If you did that lesson, you can reuse its tables; here the code that builds them is again, so this lesson works on its own. It adds the tables to your browser's copy; run the examples from top to bottom, and Reset data starts again.
CREATE TABLE dim_date (date_key INTEGER PRIMARY KEY, full_date TEXT, month INTEGER, month_name TEXT, quarter INTEGER, year INTEGER);
CREATE TABLE dim_product (product_key INTEGER PRIMARY KEY, name TEXT, category TEXT, brand TEXT);
CREATE TABLE dim_store (store_key INTEGER PRIMARY KEY, name TEXT, city TEXT, region TEXT);
CREATE TABLE fact_sales (date_key INTEGER, product_key INTEGER, store_key INTEGER, quantity INTEGER, amount INTEGER);
INSERT INTO dim_product VALUES
(1, 'Tea 250g', 'Grocery', 'Hill'), (2, 'Rice 5kg', 'Grocery', 'Delta'),
(3, 'Notebook', 'Stationery', 'Page'), (4, 'Pen box', 'Stationery', 'Page'),
(5, 'Lamp', 'Home', 'Glow'), (6, 'Kettle', 'Home', 'Glow');
INSERT INTO dim_store VALUES
(1, 'Gulshan', 'Dhaka', 'Central'), (2, 'Mirpur', 'Dhaka', 'Central'), (3, 'Agrabad', 'Chattogram', 'South-East');
INSERT INTO dim_date
WITH RECURSIVE days(d) AS (SELECT date('2026-01-01') UNION ALL SELECT date(d, '+1 day') FROM days WHERE d < '2026-06-30')
SELECT CAST(strftime('%Y%m%d', d) AS INTEGER), d, CAST(strftime('%m', d) AS INTEGER),
substr('JanFebMarAprMayJun', (CAST(strftime('%m', d) AS INTEGER) - 1) * 3 + 1, 3),
(CAST(strftime('%m', d) AS INTEGER) - 1) / 3 + 1, CAST(strftime('%Y', d) AS INTEGER)
FROM days;
INSERT INTO fact_sales
WITH RECURSIVE days(i, d) AS (SELECT 0, date('2026-01-01') UNION ALL SELECT i + 1, date(d, '+1 day') FROM days WHERE d < '2026-06-30')
SELECT CAST(strftime('%Y%m%d', d) AS INTEGER), p.product_key, s.store_key,
(i + p.product_key * s.store_key) % 5 + 1,
((i + p.product_key * s.store_key) % 5 + 1) *
CASE p.product_key WHEN 1 THEN 120 WHEN 2 THEN 450 WHEN 3 THEN 60 WHEN 4 THEN 90 WHEN 5 THEN 700 ELSE 1200 END
FROM days CROSS JOIN dim_product p CROSS JOIN dim_store s
WHERE (i + p.product_key + s.store_key) % 4 = 0;
SELECT 'dim_date' AS table_name, COUNT(*) AS row_count FROM dim_date
UNION ALL SELECT 'dim_product', COUNT(*) FROM dim_product
UNION ALL SELECT 'dim_store', COUNT(*) FROM dim_store
UNION ALL SELECT 'fact_sales', COUNT(*) FROM fact_sales;
table_name row_count
----------- ---------
dim_date 181
dim_product 6
dim_store 3
fact_sales 815
(4 rows)
CREATE TABLE dim_date (date_key INTEGER PRIMARY KEY, full_date TEXT, month INTEGER, month_name TEXT, quarter INTEGER, year INTEGER);
CREATE TABLE dim_product (product_key INTEGER PRIMARY KEY, name TEXT, category TEXT, brand TEXT);
CREATE TABLE dim_store (store_key INTEGER PRIMARY KEY, name TEXT, city TEXT, region TEXT);
CREATE TABLE fact_sales (date_key INTEGER, product_key INTEGER, store_key INTEGER, quantity INTEGER, amount INTEGER);
INSERT INTO dim_product VALUES
(1, 'Tea 250g', 'Grocery', 'Hill'), (2, 'Rice 5kg', 'Grocery', 'Delta'),
(3, 'Notebook', 'Stationery', 'Page'), (4, 'Pen box', 'Stationery', 'Page'),
(5, 'Lamp', 'Home', 'Glow'), (6, 'Kettle', 'Home', 'Glow');
INSERT INTO dim_store VALUES
(1, 'Gulshan', 'Dhaka', 'Central'), (2, 'Mirpur', 'Dhaka', 'Central'), (3, 'Agrabad', 'Chattogram', 'South-East');
INSERT INTO dim_date
WITH RECURSIVE days(d) AS (SELECT date('2026-01-01') UNION ALL SELECT date(d, '+1 day') FROM days WHERE d < '2026-06-30')
SELECT CAST(strftime('%Y%m%d', d) AS INTEGER), d, CAST(strftime('%m', d) AS INTEGER),
substr('JanFebMarAprMayJun', (CAST(strftime('%m', d) AS INTEGER) - 1) * 3 + 1, 3),
(CAST(strftime('%m', d) AS INTEGER) - 1) / 3 + 1, CAST(strftime('%Y', d) AS INTEGER)
FROM days;
INSERT INTO fact_sales
WITH RECURSIVE days(i, d) AS (SELECT 0, date('2026-01-01') UNION ALL SELECT i + 1, date(d, '+1 day') FROM days WHERE d < '2026-06-30')
SELECT CAST(strftime('%Y%m%d', d) AS INTEGER), p.product_key, s.store_key,
(i + p.product_key * s.store_key) % 5 + 1,
((i + p.product_key * s.store_key) % 5 + 1) *
CASE p.product_key WHEN 1 THEN 120 WHEN 2 THEN 450 WHEN 3 THEN 60 WHEN 4 THEN 90 WHEN 5 THEN 700 ELSE 1200 END
FROM days CROSS JOIN dim_product p CROSS JOIN dim_store s
WHERE (i + p.product_key + s.store_key) % 4 = 0;
SELECT 'dim_date' AS table_name, COUNT(*) AS row_count FROM dim_date
UNION ALL SELECT 'dim_product', COUNT(*) FROM dim_product
UNION ALL SELECT 'dim_store', COUNT(*) FROM dim_store
UNION ALL SELECT 'fact_sales', COUNT(*) FROM fact_sales;
table_name row_count
----------- ---------
dim_date 181
dim_product 6
dim_store 3
fact_sales 815
(4 rows)
CREATE TABLE dim_date (date_key INTEGER PRIMARY KEY, full_date TEXT, month INTEGER, month_name TEXT, quarter INTEGER, year INTEGER);
CREATE TABLE dim_product (product_key INTEGER PRIMARY KEY, name TEXT, category TEXT, brand TEXT);
CREATE TABLE dim_store (store_key INTEGER PRIMARY KEY, name TEXT, city TEXT, region TEXT);
CREATE TABLE fact_sales (date_key INTEGER, product_key INTEGER, store_key INTEGER, quantity INTEGER, amount INTEGER);
INSERT INTO dim_product VALUES
(1, 'Tea 250g', 'Grocery', 'Hill'), (2, 'Rice 5kg', 'Grocery', 'Delta'),
(3, 'Notebook', 'Stationery', 'Page'), (4, 'Pen box', 'Stationery', 'Page'),
(5, 'Lamp', 'Home', 'Glow'), (6, 'Kettle', 'Home', 'Glow');
INSERT INTO dim_store VALUES
(1, 'Gulshan', 'Dhaka', 'Central'), (2, 'Mirpur', 'Dhaka', 'Central'), (3, 'Agrabad', 'Chattogram', 'South-East');
INSERT INTO dim_date
WITH RECURSIVE days(d) AS (SELECT date('2026-01-01') UNION ALL SELECT date(d, '+1 day') FROM days WHERE d < '2026-06-30')
SELECT CAST(strftime('%Y%m%d', d) AS INTEGER), d, CAST(strftime('%m', d) AS INTEGER),
substr('JanFebMarAprMayJun', (CAST(strftime('%m', d) AS INTEGER) - 1) * 3 + 1, 3),
(CAST(strftime('%m', d) AS INTEGER) - 1) / 3 + 1, CAST(strftime('%Y', d) AS INTEGER)
FROM days;
INSERT INTO fact_sales
WITH RECURSIVE days(i, d) AS (SELECT 0, date('2026-01-01') UNION ALL SELECT i + 1, date(d, '+1 day') FROM days WHERE d < '2026-06-30')
SELECT CAST(strftime('%Y%m%d', d) AS INTEGER), p.product_key, s.store_key,
(i + p.product_key * s.store_key) % 5 + 1,
((i + p.product_key * s.store_key) % 5 + 1) *
CASE p.product_key WHEN 1 THEN 120 WHEN 2 THEN 450 WHEN 3 THEN 60 WHEN 4 THEN 90 WHEN 5 THEN 700 ELSE 1200 END
FROM days CROSS JOIN dim_product p CROSS JOIN dim_store s
WHERE (i + p.product_key + s.store_key) % 4 = 0;
SELECT 'dim_date' AS table_name, COUNT(*) AS row_count FROM dim_date
UNION ALL SELECT 'dim_product', COUNT(*) FROM dim_product
UNION ALL SELECT 'dim_store', COUNT(*) FROM dim_store
UNION ALL SELECT 'fact_sales', COUNT(*) FROM fact_sales;
table_name row_count
----------- ---------
dim_date 181
dim_product 6
dim_store 3
fact_sales 815
(4 rows)
2. Roll-up and drill-down
Dimensions often have levels: day → month → quarter → year, or store → city → region. Moving to a coarser level is a roll-up; moving to a finer level is a drill-down.
Roll-up from days to months, to quarters and to the grand total:
SELECT d.month_name, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_date d ON d.date_key = f.date_key
GROUP BY d.month ORDER BY d.month;
month_name amount
---------- ------
Jan 173160
Feb 176070
Mar 176030
Apr 182140
May 173160
Jun 187810
(6 rows)
SELECT d.quarter, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_date d ON d.date_key = f.date_key
GROUP BY d.quarter ORDER BY d.quarter;
quarter amount
------- ------
1 525260
2 543110
(2 rows)
SELECT SUM(amount) AS grand_total FROM fact_sales;
grand_total
-----------
1068370
(1 row)
SELECT d.month_name, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_date d ON d.date_key = f.date_key
GROUP BY d.month ORDER BY d.month;
month_name amount
---------- ------
Jan 173160
Feb 176070
Mar 176030
Apr 182140
May 173160
Jun 187810
(6 rows)
SELECT d.quarter, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_date d ON d.date_key = f.date_key
GROUP BY d.quarter ORDER BY d.quarter;
quarter amount
------- ------
1 525260
2 543110
(2 rows)
SELECT SUM(amount) AS grand_total FROM fact_sales;
grand_total
-----------
1068370
(1 row)
SELECT d.month_name, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_date d ON d.date_key = f.date_key
GROUP BY d.month ORDER BY d.month;
month_name amount
---------- ------
Jan 173160
Feb 176070
Mar 176030
Apr 182140
May 173160
Jun 187810
(6 rows)
SELECT d.quarter, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_date d ON d.date_key = f.date_key
GROUP BY d.quarter ORDER BY d.quarter;
quarter amount
------- ------
1 525260
2 543110
(2 rows)
SELECT SUM(amount) AS grand_total FROM fact_sales;
grand_total
-----------
1068370
(1 row)
The numbers agree at every level: January + February + March = 173,160 + 176,070 + 176,030 = 525,260, which is quarter 1. A roll-up must never change the total.
Now drill down from quarter 1 to its months, and split each month by region:
SELECT d.month_name, s.region, SUM(f.amount) AS amount FROM fact_sales f
JOIN dim_date d ON d.date_key = f.date_key JOIN dim_store s ON s.store_key = f.store_key
WHERE d.quarter = 1 GROUP BY d.month, s.region ORDER BY d.month, s.region;
month_name region amount
---------- ---------- ------
Jan Central 116840
Jan South-East 56320
Feb Central 118540
Feb South-East 57530
Mar Central 115960
Mar South-East 60070
(6 rows)
SELECT d.month_name, s.region, SUM(f.amount) AS amount FROM fact_sales f
JOIN dim_date d ON d.date_key = f.date_key JOIN dim_store s ON s.store_key = f.store_key
WHERE d.quarter = 1 GROUP BY d.month, s.region ORDER BY d.month, s.region;
month_name region amount
---------- ---------- ------
Jan Central 116840
Jan South-East 56320
Feb Central 118540
Feb South-East 57530
Mar Central 115960
Mar South-East 60070
(6 rows)
SELECT d.month_name, s.region, SUM(f.amount) AS amount FROM fact_sales f
JOIN dim_date d ON d.date_key = f.date_key JOIN dim_store s ON s.store_key = f.store_key
WHERE d.quarter = 1 GROUP BY d.month, s.region ORDER BY d.month, s.region;
month_name region amount
---------- ---------- ------
Jan Central 116840
Jan South-East 56320
Feb Central 118540
Feb South-East 57530
Mar Central 115960
Mar South-East 60070
(6 rows)
3. Slice and dice
- A slice fixes one value of one dimension: "only the city Dhaka".
- A dice picks several values of several dimensions: "only quarter 2, only Home and Stationery".
Slice: one value (city = Dhaka), then look at it by category:
SELECT p.category, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_store s ON s.store_key = f.store_key
JOIN dim_product p ON p.product_key = f.product_key WHERE s.city = 'Dhaka' GROUP BY p.category ORDER BY p.category;
category amount
---------- ------
Grocery 156150
Home 516600
Stationery 40740
(3 rows)
Dice: several values at once, then look at it by store:
SELECT s.name AS store, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_store s ON s.store_key = f.store_key
JOIN dim_product p ON p.product_key = f.product_key JOIN dim_date d ON d.date_key = f.date_key
WHERE d.quarter = 2 AND p.category IN ('Home', 'Stationery') GROUP BY s.name ORDER BY s.name;
store amount
------- ------
Agrabad 142860
Gulshan 142300
Mirpur 143470
(3 rows)
Slice: one value (city = Dhaka), then look at it by category:
SELECT p.category, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_store s ON s.store_key = f.store_key
JOIN dim_product p ON p.product_key = f.product_key WHERE s.city = 'Dhaka' GROUP BY p.category ORDER BY p.category;
category amount
---------- ------
Grocery 156150
Home 516600
Stationery 40740
(3 rows)
Dice: several values at once, then look at it by store:
SELECT s.name AS store, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_store s ON s.store_key = f.store_key
JOIN dim_product p ON p.product_key = f.product_key JOIN dim_date d ON d.date_key = f.date_key
WHERE d.quarter = 2 AND p.category IN ('Home', 'Stationery') GROUP BY s.name ORDER BY s.name;
store amount
------- ------
Agrabad 142860
Gulshan 142300
Mirpur 143470
(3 rows)
Slice: one value (city = Dhaka), then look at it by category:
SELECT p.category, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_store s ON s.store_key = f.store_key
JOIN dim_product p ON p.product_key = f.product_key WHERE s.city = 'Dhaka' GROUP BY p.category ORDER BY p.category;
category amount
---------- ------
Grocery 156150
Home 516600
Stationery 40740
(3 rows)
Dice: several values at once, then look at it by store:
SELECT s.name AS store, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_store s ON s.store_key = f.store_key
JOIN dim_product p ON p.product_key = f.product_key JOIN dim_date d ON d.date_key = f.date_key
WHERE d.quarter = 2 AND p.category IN ('Home', 'Stationery') GROUP BY s.name ORDER BY s.name;
store amount
------- ------
Agrabad 142860
Gulshan 142300
Mirpur 143470
(3 rows)
In SQL a slice or a dice is just a WHERE clause on dimension columns; the operations are named because analysts use them all the time in reporting tools.
4. The snowflake schema
A snowflake schema normalizes the dimensions: a dimension is split into sub-tables. Here the category moves out of dim_product into its own table:
dim_category ── dim_product ── fact_sales ── dim_store
|
dim_date
CREATE TABLE dim_category (category_key INTEGER PRIMARY KEY, category TEXT);
INSERT INTO dim_category (category) SELECT DISTINCT category FROM dim_product ORDER BY category;
CREATE TABLE dim_product_sf AS
SELECT p.product_key, p.name, c.category_key, p.brand FROM dim_product p JOIN dim_category c ON c.category = p.category;
SELECT c.category, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_product_sf p ON p.product_key = f.product_key
JOIN dim_category c ON c.category_key = p.category_key GROUP BY c.category ORDER BY c.category;
category amount
---------- ------
Grocery 233580
Home 773800
Stationery 60990
(3 rows)
CREATE TABLE dim_category (category_key INTEGER PRIMARY KEY, category TEXT);
INSERT INTO dim_category (category) SELECT DISTINCT category FROM dim_product ORDER BY category;
CREATE TABLE dim_product_sf AS
SELECT p.product_key, p.name, c.category_key, p.brand FROM dim_product p JOIN dim_category c ON c.category = p.category;
SELECT c.category, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_product_sf p ON p.product_key = f.product_key
JOIN dim_category c ON c.category_key = p.category_key GROUP BY c.category ORDER BY c.category;
category amount
---------- ------
Grocery 233580
Home 773800
Stationery 60990
(3 rows)
CREATE TABLE dim_category (category_key INTEGER PRIMARY KEY, category TEXT);
INSERT INTO dim_category (category) SELECT DISTINCT category FROM dim_product ORDER BY category;
CREATE TABLE dim_product_sf AS
SELECT p.product_key, p.name, c.category_key, p.brand FROM dim_product p JOIN dim_category c ON c.category = p.category;
SELECT c.category, SUM(f.amount) AS amount FROM fact_sales f JOIN dim_product_sf p ON p.product_key = f.product_key
JOIN dim_category c ON c.category_key = p.category_key GROUP BY c.category ORDER BY c.category;
category amount
---------- ------
Grocery 233580
Home 773800
Stationery 60990
(3 rows)
The answer is the same as before (compare lesson 13.2's category totals), but it took one extra join.
| Star | Snowflake | |
|---|---|---|
| Dimension tables | one flat table per dimension | split into several linked tables |
| Joins per query | fewer | more |
| Repeated data | some (category names repeated) | little |
| Easier to understand and query | yes | less |
| Storage and update effort | slightly more storage | a category is renamed in one place |
Most warehouses choose the star because queries are simpler and the extra storage is small. The snowflake is used when a dimension is very large or shared (for example one category table used by several dimensions) and keeping one copy matters more than fewer joins.
Worked example: answer a question from the star
Question: "Which brand sold most in the South-East region in the second quarter?"
- Measure: amount, from
fact_sales. - Dimensions needed: brand (
dim_product), region (dim_store), quarter (dim_date). - Slice and dice: region = 'South-East' and quarter = 2.
- Group by: brand. Sum the amount.
SELECT p.brand, SUM(f.amount) AS amount FROM fact_sales f
JOIN dim_product p ON p.product_key = f.product_key
JOIN dim_store s ON s.store_key = f.store_key
JOIN dim_date d ON d.date_key = f.date_key
WHERE s.region = 'South-East' AND d.quarter = 2
GROUP BY p.brand ORDER BY amount DESC;
brand amount
----- ------
Glow 132600
Delta 29700
Page 10260
Hill 8400
(4 rows)
SELECT p.brand, SUM(f.amount) AS amount FROM fact_sales f
JOIN dim_product p ON p.product_key = f.product_key
JOIN dim_store s ON s.store_key = f.store_key
JOIN dim_date d ON d.date_key = f.date_key
WHERE s.region = 'South-East' AND d.quarter = 2
GROUP BY p.brand ORDER BY amount DESC;
brand amount
----- ------
Glow 132600
Delta 29700
Page 10260
Hill 8400
(4 rows)
SELECT p.brand, SUM(f.amount) AS amount FROM fact_sales f
JOIN dim_product p ON p.product_key = f.product_key
JOIN dim_store s ON s.store_key = f.store_key
JOIN dim_date d ON d.date_key = f.date_key
WHERE s.region = 'South-East' AND d.quarter = 2
GROUP BY p.brand ORDER BY amount DESC;
brand amount
----- ------
Glow 132600
Delta 29700
Page 10260
Hill 8400
(4 rows)
The answer: Glow. Each step maps to one part of the query: measure, joins, filters, grouping. Every star-schema question can be built in the same four steps.
Common mistakes
- Normalizing dimensions out of habit. In a warehouse, flat dimensions are usually the right choice.
- Forgetting a join condition, which multiplies rows and inflates totals.
- Summing a measure that should not be added (for example an account balance across months).
- Mixing levels in one
GROUP BYwithout noticing the totals no longer add up (such as month and quarter in the same sum).
Key takeaways
- A star schema is a fact table surrounded by flat dimension tables; a snowflake splits the dimensions into sub-tables.
- The star needs fewer joins; the snowflake stores less repeated data.
- Roll-up moves to a coarser level, drill-down to a finer one; totals must stay equal.
- Slice fixes one dimension value; dice picks several.
- Every question is: measure, dimensions, filters, grouping.
Practice
1. Which is a roll-up: month to quarter, or quarter to month?
Show answer
Month to quarter (coarser level). Quarter to month is a drill-down.
2. Is "only the Gulshan store" a slice or a dice? And "Gulshan and Mirpur, in April and May"?
Show answer
The first is a slice (one value); the second is a dice (several values on several dimensions).
3. Why do months of 173,160 + 176,070 + 176,030 have to equal 525,260?
Show answer
Quarter 1 is made up of exactly those three months, so rolling up must keep the total. If it does not, a join or filter is wrong.
4. Give one advantage of the snowflake over the star, and one disadvantage.
Show answer
Advantage: less repeated data (a name is changed in one place). Disadvantage: more joins, so queries are longer and usually slower.
5. Write the four steps you would follow to answer "total quantity of Home products sold in Dhaka stores in May".
Show answer
Measure: quantity (fact_sales). Dimensions: product category, store city, date month. Filters: category = Home, city = Dhaka, month = May. Group by: nothing (one total), or group by store for a per-store split.
6. Why is repeating "Grocery" on every grocery product acceptable in dim_product?
Show answer
The dimension is small and rarely changes, so the repeated text costs little and saves a join in every query. Warehouses trade strict normalization for simpler, faster reading.