Why Analytics Needs a Different Shape of Data
Why the normalised tables that run a shop make poor reporting tables, what facts and dimensions are, how the same question looks on a star schema, and where snowflakes, one big table, Kimball, Inmon and Data Vault fit.
Contentsand lesson path
On this page
- Why it matters
- The operational shape: one fact in one place
- The analytical shape: facts in the middle, dimensions around them
- Star, snowflake or one big table
- The quiet problem: history
- Check your understanding
- Apply it
- Review prompt
Lessons of Dimensional Modeling
- Why Analytics Needs a Different Shape of Data Concept · 15 min
- Grain First: Designing Fact Tables Concept · 25 min
- Dimensions, Surrogate Keys and Conformance Concept · 20 min
- Worked Example: A Star Schema for Online Retail Worked example · 30 min
- Lab: Type 2 History and Point-in-Time Joins Lab · 30 min
- Check: Model a New Business Process Knowledge check · 8 questions
What you will learn
- Declare the grain of a business process and design transaction, periodic-snapshot or accumulating-snapshot fact tables with correctly classified additive, semi-additive and non-additive measures.
- Design dimension tables with surrogate keys and hierarchies, and choose between star, snowflake and one-big-table layouts for a use case.
Prerequisites: Relational Data Modeling
01. Why it matters
An online shop runs on an OLTP database (online transaction processing). It handles many small reads and writes: place an order, change an address, check stock. Its tables are normalised, which means each fact is stored once and everything else points to it by key, so an update touches one row and cannot leave two copies disagreeing.
Analysts ask a different kind of question: "revenue by category and country, per month, for the last two years". That is OLAP work (online analytical processing): read millions of rows, group them by descriptive attributes, add up numbers. On normalised tables such a question needs many joins, and it may give wrong answers about the past, because OLTP rows are updated in place. Dimensional modelling reshapes the same data for this kind of reading. This lesson shows the two shapes side by side on a tiny shop. You need the sql-lab PostgreSQL 17 container from SQL Fundamentals.
02. The operational shape: one fact in one place
Save the script as oltp.sql. Cities, customers, categories, products, orders and order lines each have their own table, as a well-designed shop database would.
-- the shop's operational (OLTP) tables, normalised
CREATE TABLE cities (city_id int PRIMARY KEY, name text, country text);
CREATE TABLE customers (customer_id int PRIMARY KEY, name text,
city_id int REFERENCES cities);
CREATE TABLE categories (category_id int PRIMARY KEY, name text);
CREATE TABLE products (product_id int PRIMARY KEY, name text,
category_id int REFERENCES categories);
CREATE TABLE orders (order_id int PRIMARY KEY,
customer_id int REFERENCES customers, ordered_at date);
CREATE TABLE order_lines (order_id int REFERENCES orders, line_no int,
product_id int REFERENCES products,
qty int, unit_price numeric(8,2),
PRIMARY KEY (order_id, line_no));
INSERT INTO cities VALUES (1, 'Leeds', 'UK'), (2, 'Lyon', 'FR');
INSERT INTO customers VALUES (10, 'Ana', 1), (11, 'Ben', 2), (12, 'Cai', 1);
INSERT INTO categories VALUES (100, 'Books'), (101, 'Games');
INSERT INTO products VALUES (1000, 'Atlas', 100), (1001, 'Chess set', 101),
(1002, 'Novel', 100);
INSERT INTO orders VALUES (1, 10, '2026-01-14'), (2, 11, '2026-01-20'),
(3, 12, '2026-02-03'), (4, 10, '2026-02-17');
INSERT INTO order_lines VALUES
(1, 1, 1000, 1, 30), (1, 2, 1001, 1, 45), (2, 1, 1002, 2, 12),
(3, 1, 1001, 1, 45), (4, 1, 1000, 2, 30), (4, 2, 1002, 1, 12);
SELECT count(*) AS order_lines FROM order_lines;docker exec sql-lab psql -U postgres -X -q -c "SET client_min_messages = warning" \
-c "DROP DATABASE IF EXISTS shape" -c "CREATE DATABASE shape"
docker exec -i sql-lab psql -U postgres -d shape -X -q < oltp.sql order_lines
-------------
6
(1 row)Now ask the analyst's question: revenue by month, category and country. Run it in psql (docker exec -it sql-lab psql -U postgres -d shape). The number lives in order_lines, but the month is on orders, the category two tables away from the product, and the country two tables away from the customer, so the query needs five joins.
SELECT date_trunc('month', o.ordered_at)::date AS month, cat.name AS category,
ci.country, sum(ol.qty * ol.unit_price) AS revenue
FROM order_lines ol
JOIN orders o ON o.order_id = ol.order_id
JOIN products p ON p.product_id = ol.product_id
JOIN categories cat ON cat.category_id = p.category_id
JOIN customers cu ON cu.customer_id = o.customer_id
JOIN cities ci ON ci.city_id = cu.city_id
GROUP BY 1, 2, 3
ORDER BY 1, 2, 3; month | category | country | revenue
------------+----------+---------+---------
2026-01-01 | Books | FR | 24.00
2026-01-01 | Books | UK | 30.00
2026-01-01 | Games | UK | 45.00
2026-02-01 | Books | UK | 72.00
2026-02-01 | Games | UK | 45.00
(5 rows)Five joins are manageable here. In a real shop the same question also needs discounts, currencies, returns and a calendar of fiscal periods, each a few more joins, and every analyst writes them slightly differently. There is also a quieter problem, which you will see at the end of the lesson: the OLTP tables keep only the current state.
03. The analytical shape: facts in the middle, dimensions around them
A dimensional model splits data into two kinds of table:
- A fact table records measurements of a business process, one row per event at a declared level of detail called the grain. Here the grain is "one row per order line", and the measurements, called measures, are quantity and revenue.
- A dimension table holds the descriptive context used to filter and group facts: who, what, when, where. Here: date, product and customer. Attributes that were spread over several OLTP tables, such as the category of a product or the country of a customer, are copied into one wide dimension row.
Every fact row carries a foreign key to each dimension. Drawn on paper, the fact table sits in the middle with dimensions around it, which is why this layout is called a star schema. The keys here are surrogate keys, small integers the warehouse assigns itself; lesson 3 explains why. Save the script as star.sql and run it the same way as oltp.sql.
CREATE TABLE dim_date AS
SELECT to_char(d, 'YYYYMMDD')::int AS date_key, d::date AS full_date,
date_trunc('month', d)::date AS month, extract(isodow FROM d)::int AS weekday
FROM generate_series(date '2026-01-01', date '2026-02-28', interval '1 day') AS d;
CREATE TABLE dim_product AS
SELECT row_number() OVER (ORDER BY p.product_id)::int AS product_key,
p.product_id, p.name AS product, c.name AS category
FROM products p JOIN categories c USING (category_id);
CREATE TABLE dim_customer AS
SELECT row_number() OVER (ORDER BY cu.customer_id)::int AS customer_key,
cu.customer_id, cu.name AS customer, ci.name AS city, ci.country
FROM customers cu JOIN cities ci USING (city_id);
CREATE TABLE fact_sales AS
SELECT to_char(o.ordered_at, 'YYYYMMDD')::int AS date_key,
dp.product_key, dc.customer_key, o.order_id, ol.line_no,
ol.qty, ol.qty * ol.unit_price AS revenue
FROM order_lines ol
JOIN orders o ON o.order_id = ol.order_id
JOIN dim_product dp ON dp.product_id = ol.product_id
JOIN dim_customer dc ON dc.customer_id = o.customer_id;
SELECT d.month, p.category, c.country, sum(f.revenue) AS revenue
FROM fact_sales f
JOIN dim_date d ON d.date_key = f.date_key
JOIN dim_product p ON p.product_key = f.product_key
JOIN dim_customer c ON c.customer_key = f.customer_key
GROUP BY 1, 2, 3
ORDER BY 1, 2, 3;docker exec -i sql-lab psql -U postgres -d shape -X -q < star.sql month | category | country | revenue
------------+----------+---------+---------
2026-01-01 | Books | FR | 24.00
2026-01-01 | Books | UK | 30.00
2026-01-01 | Games | UK | 45.00
2026-02-01 | Books | UK | 72.00
2026-02-01 | Games | UK | 45.00
(5 rows)The answer is the same, but the query has a fixed, simple pattern: start from the fact, join each dimension once by its key, group by dimension attributes, sum measures. Business users and BI tools can follow that pattern for any question, and columnar warehouse engines are built to run it fast. The cost is redundancy: "Books" is now repeated on every book row of dim_product. Dimensions are small compared with facts, so that is usually a good trade.
04. Star, snowflake or one big table
The star is not the only analytical layout.
- A snowflake schema normalises dimensions again:
dim_productwould point to a separatedim_categorytable. It removes repetition but adds joins, and it gives little benefit on modern storage. - One big table (OBT) goes the other way and pre-joins facts and dimension attributes into a single wide table. Queries need no joins at all, which suits a single dashboard on a columnar engine. But every attribute is copied onto every fact row, so changing a dimension attribute means rewriting fact rows, and several processes cannot easily share consistent dimensions.
The query below builds one from the star and asks the same question with no joins.
CREATE TABLE sales_obt AS
SELECT d.full_date, d.month, p.product, p.category, c.customer, c.city, c.country,
f.order_id, f.qty, f.revenue
FROM fact_sales f
JOIN dim_date d ON d.date_key = f.date_key
JOIN dim_product p ON p.product_key = f.product_key
JOIN dim_customer c ON c.customer_key = f.customer_key;
SELECT month, category, country, sum(revenue) AS revenue
FROM sales_obt GROUP BY 1, 2, 3 ORDER BY 1, 2, 3 LIMIT 2; month | category | country | revenue
------------+----------+---------+---------
2026-01-01 | Books | FR | 24.00
2026-01-01 | Books | UK | 30.00
(2 rows)Teams also differ in how they organise a whole warehouse. In the Kimball approach you build dimensional models process by process, and share ("conform") the dimensions between them, so the marts add up to one consistent warehouse. In the Inmon approach you first build a normalised enterprise data warehouse, then derive dimensional marts from it. Data Vault stores raw, auditable history from many sources in hubs (business keys), links (relationships) and satellites (changing attributes), and builds star schemas on top for reporting. All three usually end with facts and dimensions where people query.
05. The quiet problem: history
Ana moves from Leeds to Lyon, and the shop updates her row, as an OLTP system should. Run the OLTP query again.
UPDATE customers SET city_id = 2 WHERE customer_id = 10;
SELECT date_trunc('month', o.ordered_at)::date AS month, ci.country,
sum(ol.qty * ol.unit_price) AS revenue
FROM order_lines ol
JOIN orders o ON o.order_id = ol.order_id
JOIN customers cu ON cu.customer_id = o.customer_id
JOIN cities ci ON ci.city_id = cu.city_id
WHERE o.ordered_at < date '2026-02-01'
GROUP BY 1, 2 ORDER BY 1, 2; month | country | revenue
------------+---------+---------
2026-01-01 | FR | 99.00
(1 row)Before the update, January was 75 in the UK and 24 in France. Now all 99 appears under France: an old report, re-run, gives a different answer, although nothing happened in January. The OLTP database is right to keep only the current address; recording history is the warehouse's job. Lesson 5 shows how a dimension keeps several versions of a customer so that past facts stay attached to the city that was true at the time.
06. Check your understanding
07. Apply it
The practice project for this skill is a star schema for a subscription business with sign-ups, payments and plan changes. Prepare for it:
- Sketch the OLTP tables such a business would have (customers, plans, subscriptions, payments) and the joins a "revenue by plan per month" question would need.
- List which tables would become facts and which would become dimensions, and what one fact row would represent.
- Write down one question about the past that the OLTP tables would answer wrongly after a customer changes plan.
08. Review prompt
Without looking back, explain:
- why OLTP tables are normalised, and why that makes analytical queries long;
- what a fact table, a measure, a dimension table and a grain are;
- the difference between a star, a snowflake and one big table, with one trade-off each;
- why an OLTP database can change the answer to a question about last year.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.