Skip to content

SQL Star Schema and Data Modelling Basics

Data & AI6 min read

Learn how star schemas work: facts, dimensions and grain explained with a worked SQL example, plus common modelling mistakes to avoid in reporting.

In this article
  1. Why operational tables are a poor fit for reporting
  2. The building blocks: facts, dimensions and grain
  3. A worked example: orders
  4. Keys, history and slowly changing dimensions
  5. Star versus snowflake versus one big table
  6. Common mistakes to avoid
  7. Data quality, governance and the Irish context
  8. Where the star fits in the wider data platform
  9. What to do next

A star schema is a way of organising analytical data into one central fact table (the events you measure, such as orders or support tickets) surrounded by dimension tables (the descriptive context, such as customer, product and date). You use it because reporting queries become simpler, faster and far easier for non-specialists to understand than they are against a copy of your operational database. If you remember one thing: decide what one row in the fact table means before you build anything else.

Why operational tables are a poor fit for reporting

Your application database is normalised to protect writes. Each fact lives in one place, so an order touches many small tables: orders, order lines, products, categories, customers, addresses, and so on. That design is excellent for keeping data consistent while people place orders. It is awkward for answering "what was revenue by product category and region last quarter?", because the query has to join half the schema and the logic is easy to get subtly wrong.

Analytical models flip the priority. Reads dominate, queries scan lots of rows, and the people writing them are often analysts rather than application developers. A star schema trades some duplication for clarity. It is the foundation under most BI tools and warehouses, and it sits naturally at the end of the pipelines described in our guide to idempotent data pipelines and backfills.

The building blocks: facts, dimensions and grain

Fact tables

A fact table records a business event or a measurement. Each row holds foreign keys to the relevant dimensions plus numeric measures you can add up or average, such as quantity, net amount or duration. Facts are usually narrow in columns and very long in rows.

Dimension tables

A dimension describes the "who, what, where, when" around the event. It is wide and comparatively short: a customer dimension might hold name, segment, country and account manager. Dimensions are what people filter and group by, so descriptive, human-readable columns matter more than saving space.

Grain

The grain is the exact meaning of one fact row. "One row per order line" is a grain. "One row per order" is a different grain. Pick one grain per fact table and write it down in plain language. Most modelling bugs, including double-counted revenue, come from mixing grains or from joining at the wrong level.

A worked example: orders

Suppose a business sells subscriptions and wants to report on sales. A sensible first model might look like this.

TableTypeGrain or keyTypical columns
fact_order_lineFactOne row per order lineorder_id, date_key, customer_key, product_key, quantity, net_amount
dim_customerDimensionOne row per customer versioncustomer_key, customer_id, name, segment, country
dim_productDimensionOne row per productproduct_key, sku, name, category
dim_dateDimensionOne row per calendar daydate_key, date, month, quarter, year, is_weekend

A typical question then becomes a short query:

SELECT d.year, d.quarter, p.category, SUM(f.net_amount) AS revenue
FROM fact_order_line f
JOIN dim_date d ON d.date_key = f.date_key
JOIN dim_product p ON p.product_key = f.product_key
GROUP BY d.year, d.quarter, p.category
ORDER BY d.year, d.quarter, revenue DESC;

Every question follows the same shape: join the fact to the dimensions you need, filter, group, aggregate. That predictability is the real benefit. Analysts learn the pattern once and reuse it everywhere.

Keys, history and slowly changing dimensions

Use surrogate keys, meaning simple integers generated in the warehouse, as the join keys between facts and dimensions. Keep the source system's identifier as an ordinary column too. Surrogate keys insulate the model from source changes and let you keep history.

History matters because dimensions change. A customer moves country, or a product is recategorised. There are two common choices, usually called Type 1 and Type 2:

  • Type 1: overwrite the old value. Simple, but past reports change when you rerun them.
  • Type 2: add a new row with a new surrogate key, plus validity dates and a current flag. Old facts keep pointing at the old version, so history stays truthful.

It depends on the question. If finance needs "revenue by the region the customer was in at the time", you need Type 2. If a spelling correction is all that changed, Type 1 is fine. Decide per attribute, not per table, and record the decision.

Star versus snowflake versus one big table

A snowflake schema further normalises the dimensions, for example splitting product into product, subcategory and category tables. It saves a little storage but adds joins and makes the model harder to read. Most modern warehouses make storage cheap, so a plain star is usually the better default.

Some teams flatten everything into one wide table for a particular dashboard. That can be perfectly sensible as a final serving layer, but it works best when it is built from a clean star beneath it, not instead of one.

ApproachStrengthWatch out for
Star schemaClear, fast to query, tool-friendlyNeeds upfront thought about grain
Snowflake schemaLess duplication in dimensionsMore joins, harder for analysts
One wide tableSimple for a single dashboardDuplicated logic, hard to extend

Common mistakes to avoid

  1. No stated grain. If two people describe the fact row differently, totals will disagree.
  2. Adding measures that cannot be summed. Ratios and percentages should be calculated from underlying sums at query time, not stored and then averaged.
  3. Putting descriptive text in the fact table. It belongs in a dimension, where it is stored once and can be corrected.
  4. Skipping a date dimension. Calendar logic such as fiscal quarters, bank holidays and week numbers belongs in one place, not in dozens of queries.
  5. Ignoring nulls in keys. Add an explicit "Unknown" row to each dimension so facts with a missing customer still join cleanly and are visible.
  6. Modelling for one report only. Model the business process, then build reports on top.

Data quality, governance and the Irish context

A tidy model also makes governance easier. When customer attributes live in one dimension, you know where personal data sits, who can see it, and how to remove or restrict it. Under GDPR you should only keep personal data in the warehouse that you actually need for the analysis, and be able to explain why. Consider whether an analytical question really needs names and emails, or whether a pseudonymous key would do. Our overview of data governance basics for SMEs covers ownership and access, and the Data Protection Commission (DPC) publishes guidance on the principles. This is not legal advice.

Finally, test the model like software. Check that fact keys always match a dimension row, that the grain really is unique, and that totals reconcile with the source system. These checks are cheap and catch most surprises before a director spots them in a dashboard. Once the model is stable, the next question is how people consume it, which we cover in BI dashboards people actually use.

Where the star fits in the wider data platform

A star schema is a modelling pattern, not a product. It usually sits in a serving layer: raw data lands first, gets cleaned and conformed, and is then shaped into facts and dimensions for reporting. Keeping those layers separate means you can reload the model from raw data if a rule changes. If you are choosing where the data should live and how to connect to it, our comparison of REST and GraphQL is a useful reminder that how data is exposed matters as much as how it is stored. It also pays to document each table in plain language, so a new analyst can find their way around in an afternoon rather than a month.

What to do next

  1. Pick one business process, such as sales or support tickets, and write its grain in a single sentence.
  2. List the dimensions people filter by, and the numeric measures they add up.
  3. Sketch the star on one page before writing any SQL.
  4. Build a date dimension and an "Unknown" row for each dimension.
  5. Add reconciliation tests against the source system, and keep them running.

If you would like help with this, the CloudWiss team in Dublin can talk it through, from modelling to pipelines and reporting, on our data engineering and analytics page.

Share this article

General educational information, not legal or professional advice for your specific situation.

Frequently asked questions

Is a star schema still relevant with modern cloud warehouses?

Yes. Cheap storage and fast engines remove some old performance reasons, but the main benefit is clarity. A star gives analysts a predictable shape for every question and gives BI tools a model they understand well.

Do I need a data warehouse to use a star schema?

Not strictly. You can model a star in a relational database or even as views over existing tables. A warehouse helps once data volumes grow or you combine several source systems, but the thinking about grain and dimensions applies either way.

How many fact tables should one model have?

One per business process, each with its own grain. Sales, support tickets and website sessions are usually separate facts. They can share dimensions such as date and customer, which lets you compare them side by side.

Should I store calculated measures like margin percentage?

Generally store the additive building blocks, such as revenue and cost, and calculate ratios at query or semantic layer level. Averaging stored percentages gives wrong answers when the underlying volumes differ.

Keep reading

  • Data & AI6 min read

    Data Governance Basics for SMEs

    A practical, lightweight approach to data governance for small and medium businesses: owners, a data inventory, quality rules, access and retention.

  • Data & AI6 min read

    BI Dashboards People Actually Use

    How to design BI dashboards that get opened: start from decisions, trust the numbers, cut clutter, keep them fast, and avoid the common mistakes.

Let’s build it

Ready to build with CloudWiss?

Tell us what you want to build, move to the cloud or keep running. We reply with a clear next step.