Normalization: store each fact once
Imagine ShopKart kept everything in one big sheet:
| order_id | customer | city | product | category | price |
|---|---|---|---|---|---|
| 101 | Asha | Kolkata | Headphones | Electronics | 1999 |
| 101 | Asha | Kolkata | Phone Case | Accessories | 299 |
| 103 | Asha | Kolkata | Charger | Accessories | 499 |
Asha's city is stored three times. If she moves, you must update every row; miss one and the data contradicts itself (an update anomaly). Normalization fixes this:
| Normal form | Rule (simply) | ShopKart example |
|---|---|---|
| 1NF | One value per cell; no repeating groups | Not 'Headphones, Phone Case' in one cell: one row per item |
| 2NF | Every column depends on the WHOLE key | In order_items (order_id, product_id), product name depends only on product_id → move to products |
| 3NF | No column depends on another non-key column | City depends on the customer, not the order → move to customers |
That's exactly how ShopKart's database is built (lesson 1): customers, orders, order_items, products.
Why analytics prefers a different shape
Normalized designs are perfect for writing (fast inserts, no anomalies) but analysts write big queries joining many tables. For analytics we denormalize on purpose into a star schema: fewer joins, simpler queries, faster BI.
🧩Dimensions (describe)
dim_date
- date_key
- month
- weekday
- is_weekend
dim_customer
- customer_id
- name
- city
dim_product
- product_id
- name
- category
⭐fact_sales (measure)
Grain: one row per order line · keys to every dimension
- date_key
- customer_id
- product_id
- status
- quantity
- revenue
-- Dimensions: the "who / what / when" (descriptive, short tables)
CREATE TABLE dim_date AS
SELECT d::date AS date_key,
EXTRACT(YEAR FROM d)::int AS year,
EXTRACT(MONTH FROM d)::int AS month,
TO_CHAR(d, 'Mon') AS month_name,
TO_CHAR(d, 'Dy') AS weekday,
EXTRACT(ISODOW FROM d) IN (6, 7) AS is_weekend
FROM generate_series(DATE '2026-01-01', DATE '2026-12-31', INTERVAL '1 day') AS d;
CREATE VIEW dim_customer AS SELECT customer_id, name, city FROM customers;
CREATE VIEW dim_product AS SELECT product_id, name, category FROM products;
-- Fact: one row per order line (the grain), with the numbers to add up
CREATE VIEW fact_sales AS
SELECT oi.order_id, o.order_date AS date_key, o.customer_id, oi.product_id,
o.status, oi.quantity, oi.quantity * oi.unit_price AS revenue
FROM order_items oi
JOIN orders o ON o.order_id = oi.order_id;Querying the star
SELECT d.month_name,
p.category,
SUM(f.revenue) AS revenue,
SUM(f.quantity) AS units
FROM fact_sales f
JOIN dim_date d ON d.date_key = f.date_key
JOIN dim_product p ON p.product_id = f.product_id
WHERE f.status = 'DELIVERED'
GROUP BY d.month, d.month_name, p.category
ORDER BY d.month, revenue DESC;| month_name | category | revenue | units |
|---|---|---|---|
| Apr | Electronics | 13996.00 | 4 |
| Apr | Accessories | 4192.00 | 8 |
| May | Electronics | 13996.00 | 4 |
| May | Accessories | 4992.00 | 8 |
Every analytics question has the same shape: filter and group by dimension columns, sum fact columns. A date dimension makes questions like “weekends vs weekdays” trivial:
SELECT d.is_weekend, COUNT(DISTINCT f.order_id) AS orders, SUM(f.revenue) AS revenue
FROM fact_sales f
JOIN dim_date d ON d.date_key = f.date_key
WHERE f.status = 'DELIVERED'
GROUP BY d.is_weekend;| is_weekend | orders | revenue |
|---|---|---|
| f | 10 | 28179.00 |
| t | 2 | 8997.00 |
The four steps of dimensional modeling
- Pick the business process: sales (orders).
- Declare the grain: one row per order line. Every fact row must mean exactly this.
- Choose dimensions: date, customer, product (who, what, when, where).
- Choose facts: quantity, revenue (numbers that add up).
| Term | Meaning |
|---|---|
| Grain | What one fact row represents |
| Additive fact | Can be summed across all dimensions (revenue) |
| Semi-additive | Sum across some dimensions only (account balance: not across time) |
| Surrogate key | A meaningless id made by the warehouse (customer_key), instead of the source system's id |
| Degenerate dimension | A dimension value kept in the fact with no table (order_id) |
| Conformed dimension | One dimension shared by many facts (dim_date everywhere) |
| Factless fact | Events with no numbers (logins: who logged in when) |
Slowly changing dimensions (SCD)
SCD Type 1: overwrite
No history
C02 · Rahul · Pune
SCD Type 2: new version row
Full history
C02 · Mumbai · valid until today · current = false
C02 · Pune · valid from today · current = true
Type 2 lets you report revenue by the city a customer lived in at the time of each order. We build it step by step with MERGE in the Databricks medallion lesson.
Star vs snowflake vs one big table
| Design | Shape | Good for |
|---|---|---|
| Normalized (3NF) | Many small tables | Apps (OLTP), source systems |
| Star schema | Fact + denormalized dimensions | BI and analytics (most common) |
| Snowflake schema | Fact + normalized dimensions | Very large dimensions; fewer duplicates |
| One big table (OBT) | Everything pre-joined | Simple dashboards in columnar warehouses |
🎯 Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1Explain fact and dimension tables.Fresher+
Q2What are 1NF, 2NF and 3NF?Fresher+
Q3Star vs snowflake schema?1–3 yrs+
Q4What is the grain of a fact table and why does it matter?1–3 yrs+
Q5Why use surrogate keys instead of source system ids?3+ yrs+
Q6How would you model ShopKart's orders for a BI team?3+ yrs+
Key takeaways
- ✓Normalize where data is written; denormalize into a star where it's analysed.
- ✓Declare the grain, then pick dimensions and facts.
- ✓SCD Type 2 keeps history for point-in-time reporting.
- ✓Next: the SQL patterns data engineers use in real pipelines.
Frequently asked questions
What is data modeling?
Deciding which tables to create, what each row means, which columns they have and how they connect, so data is correct, easy to query and fast for its purpose.
What is normalization?
Organising tables so each fact is stored once, removing repetition. It prevents update mistakes (changing a city in one row but not another) and is the norm for app (OLTP) databases.
What is a star schema?
An analytics design with one central fact table of events and numbers (sales) surrounded by dimension tables that describe them (customer, product, date). Diagrammed, it looks like a star.
Fact vs dimension table?
Facts are events with measures you add up (quantity, revenue), many rows, keys to dimensions. Dimensions are descriptive attributes you filter and group by (city, category, month), fewer rows.
Star schema vs snowflake schema?
In a snowflake schema, dimensions are normalised into more tables (product → category table). It saves a little space but needs more joins. Star schemas are simpler and usually preferred for BI.
🧠 Quick check: data modeling
1. Which is a FACT?
2. The 'grain' of fact_sales is...
3. Normalization mainly prevents...
4. A customer moves city and you must keep history. Use...
Checking your access…