Look at a bookshop cashier's receipt roll. Each line only prints an item code, a member code, the date, the quantity and the price. The item's name and author live in the catalogue, the member's name and city in the member list, and "which weekday, which week" in the calendar. A warehouse can lay out its tables the same way, and that layout is called a star schema. In the middle sits one fact table that records what happened with its numbers: the receipt roll. Around it are dimension tables that say who, what and when: the catalogue, the member list and the calendar. Drawn out, the fact sits in the middle with its dimensions around it, shaped like a star. The point is simple: business questions become easy to answer.
Grain first: what is one row?
Before creating any table, decide the grain: what one row of the fact table stands for. For Bookshop the grain is one book in one order, that is, one order line. The cleaned data: 18 rows, 21 books, $444.
Why not one row per order? Order 2 holds This Earth of Mankind and Beauty Is a Wound. Folded into one row, the titles are gone, and the owner's question ("which book sells best per city per week?") can't be answered any more. Pick the finest grain the source has: a fine grain can be summed into a coarse one, never the other way round.
Fact table: keys and numbers
A fact table holds two kinds of columns: keys to each dimension, and measures, numbers that can be summed.
CREATE TABLE fact_sales (
order_id INT, -- the order number, with no dimension table of its own
date_key INT, -- to dim_date, e.g. 20261001
book_key INT, -- to dim_book
customer_key INT, -- to dim_customer
qty INT, -- measure: number of books
price INT, -- measure: price per book at the time
subtotal INT -- measure: qty * price
);
It is filled from the app's tables, applying the cleaning rules from the earlier lessons:
INSERT INTO fact_sales
SELECT i.order_id,
CAST(strftime('%Y%m%d', o.created_at) AS INT),
b.book_key,
p.customer_key,
i.qty,
b.price,
i.qty * b.price
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY order_id, book_id ORDER BY id) AS rn
FROM order_items) i
JOIN orders o ON o.id = i.order_id
JOIN dim_customer p ON p.customer_id = o.customer_id
JOIN dim_book b ON b.book_id = i.book_id
AND date(o.created_at) >= b.valid_from
AND date(o.created_at) < b.valid_to
WHERE i.rn = 1;
ROW_NUMBER() ... WHERE i.rn = 1drops item 9, the doubled line from the retried job.b.pricetakes the price from the catalogue, so item 16 gets 19, not 190. Bookshop gives no discounts, so that day's catalogue price is the price paid.valid_fromandvalid_topick the version of the book that applied on the order's date. What that is for shows in the SCD section.
| order_id | date_key | book_key | customer_key | qty | price | subtotal |
|---|---|---|---|---|---|---|
| 1 | 20260928 | 1 | 1 | 1 | 18 | 18 |
| 2 | 20260928 | 2 | 2 | 1 | 25 | 25 |
| 2 | 20260928 | 3 | 2 | 1 | 22 | 22 |
| 6 | 20261001 | 2 | 1 | 2 | 25 | 50 |
It holds only keys and numbers. That is why the fact table is the one that stores metrics you can aggregate (SUM(qty), SUM(subtotal)), while the descriptive text lives in the dimensions.
Dimension tables: context people read
Bookshop's three dimensions, one row per "thing":
| Table | One row = | Columns |
|---|---|---|
dim_book |
one version of a book | book_key, book_id, title, author, price, valid_from, valid_to, is_current |
dim_date |
one day | date_key (20261001), full_date, day_name (Thursday), week (the Monday its week starts on), is_weekend |
dim_customer |
one customer | customer_key, customer_id, name, city (Maya: Unknown) |
book_keyandcustomer_keyare surrogate keys: numbers the warehouse makes, separate from the app'sid(the natural key, kept asbook_idandcustomer_id). At first the numbers happen to match. As soon as a book has two versions, onebook_idhas twobook_keys.dim_dateis built up front for every day (28 September to 11 October: 14 rows), including days without sales such as 3 October. Weekday and week are worked out once here, not in every query.authoris copied straight intodim_book, andcityintodim_customer. That is denormalisation, on purpose.
The owner's question on the star schema
WITH per_title AS (
SELECT t.week, p.city, b.title, SUM(f.qty) AS sold
FROM fact_sales f
JOIN dim_date t ON t.date_key = f.date_key
JOIN dim_customer p ON p.customer_key = f.customer_key
JOIN dim_book b ON b.book_key = f.book_key
GROUP BY t.week, p.city, b.title
),
ranking AS (
SELECT *, RANK() OVER (PARTITION BY week, city ORDER BY sold DESC) AS position
FROM per_title
)
SELECT week, city, title, sold
FROM ranking
WHERE position = 1
ORDER BY week, city, title;
The result is the same 9 rows as in the SQL lesson: London in the week of 28 September is The Rainbow Troops 3, and Manchester in the week of 5 October is a tie. The difference: cleaning, the calendar and the city names were done once, when the tables were built. The query is just the fact plus one JOIN per dimension.
Star or snowflake
If the dimensions are normalised further, so authors move to dim_author and cities to a table of their own, the shape branches out like a snowflake: a snowflake schema. "Books sold per author per city" takes 2 JOINs on the star schema (dim_book, dim_customer) and 4 on the snowflake.
In the app's database (OLTP), full normalisation (3NF) is right: each fact is stored once, so updates never leave data contradicting itself. A warehouse needs something else: it is read far more often than it is changed, and only the pipeline changes it. The star schema's advantage over 3NF: simpler queries and faster JOINs because the dimensions are denormalised. The price is that text such as "Pramoedya Ananta Toer" is written more than once. Warehouses store data column by column and compress repeated values, so it costs little.
Slowly changing dimension: a price that changes
Dimensions change rarely, but they do. On Saturday 10 October, This Earth of Mankind drops to $24, and that morning Siti buys one (order 15, at 10:15:30). How the warehouse handles such a change is called SCD (slowly changing dimension).
SCD type 1: overwrite.
UPDATE dim_book SET price = 24 WHERE book_id = 2;
Revenue for the week of 28 September was $275. Every night the pipeline rebuilds fact_sales, taking the price from dim_book. Guess first: after the pipeline runs again, what is that week's revenue?
That night the pipeline rebuilds the fact, and every This Earth of Mankind line, including the orders of 28 September, is now $24. Revenue for the week of 28 September changes by itself from $275 to $271 (4 books × $1 gone). Last week's report changed although nothing happened last week. Type 1 suits fixing a typo, not a change that has a history.
SCD type 2: add a row for the new version.
UPDATE dim_book
SET valid_to = '2026-10-10', is_current = FALSE
WHERE book_id = 2 AND is_current;
INSERT INTO dim_book
VALUES (5, 2, 'This Earth of Mankind', 'Pramoedya Ananta Toer', 24, '2026-10-10', '9999-12-31', TRUE);
| book_key | book_id | price | valid_from | valid_to | is_current |
|---|---|---|---|---|---|
| 2 | 2 | 25 | 2026-09-01 | 2026-10-10 | 0 |
| 5 | 2 | 24 | 2026-10-10 | 9999-12-31 | 1 |
- The old version is closed:
valid_togets the date of the change andis_currentbecomesFALSE.9999-12-31means "still valid". - The fact load above picks the version with
order_date >= valid_from AND order_date < valid_to. Orders before 10 October getbook_key2 ($25), order 15 getsbook_key5 ($24). - Revenue for the week of 28 September stays $275. The week of 5 October becomes $193 ($169 plus order 15).
Type 3 (keep the previous value in an extra column, such as previous_price) remembers only the last change, so it is rarely used.
⚠️ Why do the numbers double or split?
- Mixed grain. Putting a per-order shipping fee on per-book rows adds the fee up once per book. One fact table, one grain.
- JOINing a type 2 dimension without the date range.
JOIN dim_book b ON b.book_id = i.book_idalone makes every This Earth of Mankind line match both versions: the fact becomes 25 rows and 29 books instead of 19 and 22. - GROUP BY the surrogate key in a report. After type 2,
GROUP BY book_keysplits This Earth of Mankind into two rows. Group bytitleorbook_id. - Using the app's id as the dimension's key. One
book_idcan have several versions, so the app's id can't be the dimension's primary key.
TL;DR: A star schema = one fact table in the middle (keys to the dimensions + measures you can sum: qty, price, subtotal) with dimension tables around it (descriptive text: dim_book, dim_date, dim_customer). Decide the grain first; for Bookshop: one book in one order, 18 rows. Dimensions have their own surrogate key (book_key) apart from the natural key (book_id). Dimensions are denormalised (the author sits in dim_book), so a query needs only one JOIN per dimension: simpler and faster than a snowflake or 3NF. When a dimension changes: SCD type 1 overwrites and rewrites history (the week of 28 September becomes $271), SCD type 2 adds a version with valid_from, valid_to, is_current so old orders keep their old price (still $275).
⚡ Grain first
Meaning: Before creating a table, decide the grain: what one row of the fact stands for. At Bookshop, one book in one order: 18 rows. A fine grain can be summed into a coarse one, never the other way round.
⚡ Fact table: keys and numbers
Write:
CREATE TABLE fact_sales (
order_id INT, -- the order number, with no dimension table of its own
date_key INT, -- to dim_date, e.g. 20261001
book_key INT, -- to dim_book
customer_key INT, -- to dim_customer
qty INT, -- measure: number of books
price INT, -- measure: price per book at the time
subtotal INT -- measure: qty * price
);
Meaning: A fact table holds a key to each dimension and numbers you can sum (qty, price, subtotal). Text people read, such as titles and cities, lives in the dimension tables.
⚡ Denormalised dimensions
Meaning: The author is copied straight into dim_book, so the owner's question needs only the fact plus one JOIN per dimension. The query is simpler and faster than on a snowflake or 3NF.
⚡ A price changes: SCD
Guess first: This Earth of Mankind's price is UPDATEd to $24 (SCD type 1), then the fact is rebuilt. What is the revenue for the week of 28 September now?
$271, where it was $275. Last week's report changed although nothing happened last week.
Meaning: SCD type 2 adds a row for the new version with valid_from, valid_to and is_current, so old orders keep their old price.