Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Explain fact and dimension tables in a star schema using an online shop. How does a snowflake schema differ?

In a star schema, a fact table records measurable business events at one declared grain, such as one row per order line with its quantity and amount, and holds keys to dimension tables. Dimension tables, such as date, product and customer, hold the descriptive attributes we filter and group by, and each joins directly to the fact table, which keeps queries simple and fast. A snowflake schema normalises those dimensions into further tables, such as product to category to department, which saves some storage and duplication but adds joins.

Why this question is asked

Almost every analytics question in a business, how much did we sell, to whom, of what, and when, is answered from a model like this. Reporting tools, warehouses and column stores are all tuned for it. Interviewers ask about it because a developer who understands facts, dimensions and grain designs tables that give correct totals, while one who does not builds reports that quietly double count. In this answer, we will build a star schema for an online shop, explain grain, look at the dimension tables and a typical query, then turn the product dimension into a snowflake and compare the two.

1Start With the Grain

Before naming a single column, we decide what one row of the fact table means. This is called the grain. For an online shop, the most useful grain for sales is one row per order line: one product in one order. From that, we can add up to orders, days, products or customers, but we can never go below it, so we pick the most detailed level the source provides.

The grain must be stated and kept. If someone adds order-level shipping fees to the same table, one row per order next to rows per line, every SUM over the table mixes two kinds of rows and the totals are wrong. A different grain belongs in a different fact table.

2The Fact Table: Measurable Events

A fact table holds the numbers we want to add up, called measures, and a foreign key to each dimension. It is long and narrow: millions or billions of rows, few columns, mostly integers.

SQL
CREATE TABLE fact_order_line (
order_date_key int NOT NULL REFERENCES dim_date, -- 20261106
product_key int NOT NULL REFERENCES dim_product, -- 5012
customer_key int NOT NULL REFERENCES dim_customer, -- 88231
order_number text NOT NULL, -- 'SO-104233', a degenerate dimension
quantity int NOT NULL, -- 2
amount numeric(12,2) NOT NULL -- 59.98
);

Notice that the keys are small integers called surrogate keys, generated by the warehouse rather than copied from the shop's database. They keep the fact rows small and the joins cheap. Quantity and amount are additive: summing them across any dimension makes sense, which is what makes a fact table easy to use.

3Dimension Tables: The Context

Dimensions describe the who, what and when of each event. They are short and wide: thousands or millions of rows with many descriptive text columns. In a star schema they are deliberately denormalised, so dim_product carries its category name and department name directly in each row, even though many products repeat the same category.

The date dimension surprises people at first. Why a table for dates? Because reports group by things a raw date does not give us directly: month name, fiscal quarter, week of year, is it a weekend, is it a holiday. Storing these once per day, about 3,650 rows for ten years, makes every report consistent.

SQL
CREATE TABLE dim_product (
product_key int PRIMARY KEY, -- 5012
sku text, -- 'MUG-BLUE-350'
product_name text, -- 'Blue mug 350 ml'
category text, -- 'Mugs'
department text -- 'Kitchen'
);
-- sales per department per month: one join per dimension
SELECT d.year, d.month, p.department, SUM(f.amount) AS sales
FROM fact_order_line f
JOIN dim_date d ON d.date_key = f.order_date_key
JOIN dim_product p ON p.product_key = f.product_key
GROUP BY d.year, d.month, p.department;
The shop's star schema
TableKindOne row isTypical columns
fact_order_linefactone product in one orderkeys, quantity, amount
dim_datedimensionone calendar dayyear, month, weekday, is_holiday
dim_productdimensionone productsku, name, category, department
dim_customerdimensionone customersegment, country, signup channel

Every question follows the same shape: join the fact table to the dimensions we need, filter and group on dimension attributes, and sum the measures. That predictability is the main benefit of a star.

4Turning the Star Into a Snowflake

A snowflake schema normalises the dimensions. Instead of storing category and department text in every product row, dim_product holds a category_key, dim_category holds a department_key, and dim_department holds the department name. Drawn on paper, the dimensions branch outward like the arms of a snowflake.

SQL
CREATE TABLE dim_department (department_key int PRIMARY KEY, department text);
CREATE TABLE dim_category (category_key int PRIMARY KEY, category text,
department_key int REFERENCES dim_department);
CREATE TABLE dim_product (product_key int PRIMARY KEY, sku text, product_name text,
category_key int REFERENCES dim_category);
-- the same report now needs three joins for the product side
SELECT p2.department, SUM(f.amount)
FROM fact_order_line f
JOIN dim_product p ON p.product_key = f.product_key
JOIN dim_category c ON c.category_key = p.category_key
JOIN dim_department p2 ON p2.department_key = c.department_key
GROUP BY p2.department;

The snowflake saves some storage and means a department rename touches one row instead of thousands. But dimensions are small compared with the fact table, so the saving is usually tiny, and every query and every analyst now has to know the extra joins. Modern column stores compress repeated text in a dimension very well, which removes most of the storage argument. That is why most teams default to a star and snowflake only where a hierarchy is large or shared by several dimensions.

5Follow-up questions

What is a degenerate dimension?

It is a dimension attribute stored directly in the fact table with no dimension table behind it. The classic example is the order number in fact_order_line. It identifies which order a line belongs to, so we can group lines by order or count distinct orders, but everything descriptive about the order already lives in other dimensions, so a dim_order table would only hold the number itself.

Why use surrogate keys instead of the shop's product ids?

Surrogate keys are small integers owned by the warehouse. They keep facts compact, let us combine sources whose ids clash, and, importantly, let one product have several dimension rows over time when its attributes change, which natural ids cannot do.

Are all measures additive?

No. Amount and quantity can be summed across every dimension. A balance or stock level is semi-additive: we can sum it across products but not across days, where we take the last value instead. Ratios such as margin percentage are non-additive and must be computed from their additive parts after aggregation.

6Conclusion

In this answer, we built a star schema for an online shop. We first declared the grain, one row per order line, then put measurable, additive values in the fact table with surrogate keys to dimensions, and placed descriptive attributes in denormalised dimension tables joined directly to the fact. We saw that a snowflake normalises those dimensions into hierarchies, saving a little storage at the cost of extra joins, and that mixing grains in one fact table produces wrong totals.

What to say in the interviewAbout 80 seconds aloud

Open with

A star schema has a fact table in the middle and dimension tables around it. The fact table records business events at a declared grain. For a shop, I'd choose one row per order line, with keys to date, product and customer, plus measures like quantity and amount, so fact_order_line has order_date_key, product_key, customer_key, quantity and amount.

Then, if they want more

The dimensions give context: dim_date has year, month and holiday flags; dim_product has name, category and department; dim_customer has segment and country. In a star these dimensions are denormalised and each joins directly to the fact, so every report is the same shape: join, filter and group on dimension attributes, sum the measures.

The most important design decision is the grain. I declare it first and never mix grains in one fact table, because putting order-level shipping rows next to line rows makes every sum wrong. A snowflake normalises the dimensions, for example product to category to department in separate tables.

It saves a little storage and makes hierarchy changes touch one row, but it adds joins for every query, and dimensions are small anyway, so I'd default to a star. The order number is a good example of a degenerate dimension: it lives in the fact table with no dimension table.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain fact and dimension tables in a star schema using an online shop. How does a...": https://scrivio-interview-library.netlify.app/databases/analytics-and-cdc/93f4aba3a0/