A data warehouse is a central database designed for analysis, not for running an app. It collects cleaned data from many systems – sales, website, CRM, finance – so the business can answer questions with SQL and dashboards.
What You Will Learn
- OLTP vs OLAP
- Fact and dimension tables
- Star and snowflake schemas
- Slowly changing dimensions
- Popular cloud warehouses
Prerequisites
OLTP vs OLAP
| OLTP (app database) | OLAP (warehouse) | |
|---|---|---|
| Purpose | Run the business: place orders | Analyse the business: sales by month |
| Queries | Many small reads/writes | Few large scans and aggregations |
| Design | Normalized | Denormalized star schema |
| Examples | MySQL, PostgreSQL | BigQuery, Snowflake, Redshift, Synapse |
Star Schema
A fact table stores measurable events (each sale) with keys to dimension tables that describe them (date, product, store, customer).
CREATE TABLE dim_date (date_key INT PRIMARY KEY, full_date DATE, month INT, year INT);
CREATE TABLE dim_product (product_key INT PRIMARY KEY, name TEXT, category TEXT);
CREATE TABLE dim_store (store_key INT PRIMARY KEY, city TEXT, state TEXT);
CREATE TABLE fact_sales (
date_key INT REFERENCES dim_date(date_key),
product_key INT REFERENCES dim_product(product_key),
store_key INT REFERENCES dim_store(store_key),
quantity INT,
amount DECIMAL(10,2)
);
SELECT d.year, d.month, p.category, s.city, SUM(f.amount) 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_store s ON s.store_key = f.store_key
GROUP BY d.year, d.month, p.category, s.city;
Explanation: Analysts can slice the same facts by any dimension. In a snowflake schema, dimensions are further normalized (e.g. category in its own table) – saving space but needing more joins.
Slowly Changing Dimensions (SCD)
| Type | When a customer moves city… |
|---|---|
| Type 1 | Overwrite the old city (no history) |
| Type 2 | Add a new row with valid_from / valid_to dates (full history) |
| Type 3 | Keep a “previous_city” column (limited history) |
Cloud Warehouses
| Warehouse | Notes |
|---|---|
| Google BigQuery | Serverless, pay per query; BigQuery ML |
| Snowflake | Runs on AWS, Azure or GCP; separate storage and compute |
| Amazon Redshift | AWS warehouse |
| Azure Synapse / Microsoft Fabric | Microsoft analytics platform |
| Databricks SQL | Warehouse on a lakehouse (see Data Lakes) |
Series
Data Engineering series: ETL Pipelines · Apache Spark · Apache Kafka · Apache Airflow · Data Warehousing · Data Lakes · Big Data Analytics
Interview Questions
Q: Fact vs dimension table?
Facts hold measurable events and foreign keys; dimensions hold descriptive attributes.
Q: Why denormalize a warehouse?
Fewer joins make large analytical queries simpler and faster.
Practice Exercises
- Design a star schema for a college: fact_attendance with student, course and date dimensions.
- Write a query for monthly revenue by product category.
Summary
A data warehouse is an analytics database. Star schemas with fact and dimension tables, and SCDs for history, make reporting fast and consistent.
Next: Data Lakes · SQL for Analytics
Machine Learning Tutorials, Courses and Certifications