System Design · medium · Asked at Shopify

Design a data warehouse schema for an e-commerce company.

Asked in Analytics Engineer interviews, in the System Design round.

Short answer

Star schema: fact_orders + dimensions (user, product, date, geo); slowly changing dims.

How to answer it

A star schema: fact tables at the grain of a business event, surrounded by dimensions that describe the who, what, where and when.

Facts, each at one grain, stated explicitly:

Dimensions:

CREATE TABLE fact_order_items (
  order_item_key   BIGINT PRIMARY KEY,
  order_id         BIGINT,
  customer_key     BIGINT REFERENCES dim_customer,   -- version key, not natural id
  product_key      BIGINT REFERENCES dim_product,
  order_date_key   INT    REFERENCES dim_date,
  quantity         INT,
  unit_price       NUMERIC(12,2),
  discount_amount  NUMERIC(12,2)
);

The decisions to say out loud: the grain of each fact, which dimensions are SCD2, that facts reference surrogate keys so history is preserved, and that ratios (conversion rate, average order value) are computed from additive measures at query time rather than stored.

Related questions

Practice this for real