Fact vs Dimension Table
When working with data warehouse schema types, one of the most important concepts is:
๐ Fact Table vs Dimension Tableโ
Understanding this is the foundation of Star Schema, Snowflake Schema, and Data Modeling.
What is a Fact Table?โ
A Fact Table stores:
- Quantitative data (numbers)
- Business metrics like:
- sales_amount
- quantity
- revenue
Key Ideaโ
๐ Contains measurable values + foreign keys
What is a Dimension Table?โ
A Dimension Table stores:
- Descriptive data (text)
- Context about facts:
- customer_name
- product_name
- region
Key Ideaโ
๐ Adds meaning and context to facts
Fact vs Dimension Table (6 Key Differences)โ
| Feature | Fact Table | Dimension Table |
|---|---|---|
| Data Type | Numeric | Text / Descriptive |
| Purpose | Store metrics | Provide context |
| Size | Large | Smaller |
| Keys | Foreign keys | Primary keys |
| Normalization | Less important | Can be denormalized |
| Example | Sales, Transactions | Customer, Product |
Example of Fact and Dimension Tableโ
Fact Table Example (fact_sales)โ
| sale_id | customer_id | product_id | amount |
|---|---|---|---|
| 1 | 101 | 501 | 500 |
| 2 | 102 | 502 | 300 |
Dimension Table Example (dim_customer)โ
| customer_id | customer_name | city |
|---|---|---|
| 101 | John | New York |
| 102 | Alice | London |
Example Code (SQL)โ
Query Using Fact + Dimension Tablesโ
SELECT
c.customer_name,
SUM(f.amount) AS total_sales
FROM fact_sales f
JOIN dim_customer c
ON f.customer_id = c.customer_id
GROUP BY c.customer_name;
๐ Insight:
- Fact table provides numbers
- Dimension table provides meaning
When to Use Fact vs Dimension Tableโ
Use Fact Table when:โ
- Storing metrics / KPIs
- Recording transactions
- Tracking business events
Use Dimension Table when:โ
- Adding descriptive context
- Filtering & grouping data
- Supporting analytics queries
Performance Considerationโ
- Fact tables are large and heavily queried
- Dimension tables are optimized for filtering
- Proper indexing improves performance
๐ In analytics: Fact + Dimension together = Powerful insights
Common Mistakes ๐จโ
โ Mixing Fact and Dimension Dataโ
- Putting text fields in fact table
โ Too Many Columns in Fact Tableโ
- Makes queries slow
โ Not Using Surrogate Keysโ
- Causes join issues
Interview Angle ๐ฅโ
Common Questions:โ
1. What is a Fact Table?
๐ Stores measurable business data
2. What is a Dimension Table?
๐ Stores descriptive attributes
3. What is the relationship?
๐ Fact table joins dimension tables using keys
4. Can a fact table exist without dimensions?
๐ Not useful in analytics
FAQโ
What is fact table in simple terms?โ
A fact table stores measurable business data like sales or revenue.
What is dimension table?โ
A dimension table stores descriptive attributes like customer or product details.
What is difference between fact and dimension table?โ
Fact table stores numbers, while dimension table provides context.
Why are both used together?โ
To combine metrics + meaning for analysis.
Comparison Diagramโ
Fact Table
- Stores metrics
- Large volume
- Contains foreign keys
- Used in aggregations
Dimension Table
- Stores descriptive data
- Smaller size
- Contains primary keys
- Used for filtering
Final Summaryโ
- Fact Table = Numbers (metrics) ๐
- Dimension Table = Context (descriptions) ๐งฉ
๐ Together, they form the backbone of data warehouse design