Data modeling round
The prompt is usually short and vague: "Model a ride-sharing app", "Design tables for subscriptions". The interviewer is watching how you turn a business process into tables, and whether your model gives the right numbers when the data changes.
The method
- Clarify the business process and the questions. "What decisions will this support? Give me three questions an analyst will ask on day one." Write them down. You'll test your model against them at the end.
- Declare the grain of each fact table, in one sentence, before listing any columns. "One row per trip request." Most modeling mistakes are grain mistakes.
- Pick the fact type:
- Transaction: one row per event (an order line, a payment). Insert-only, fully additive.
- Periodic snapshot: one row per entity per period (account balance per day). Measures are often semi-additive.
- Accumulating snapshot: one row per process instance, updated as milestones happen (order placed → shipped → delivered).
- Factless fact: an event with no measures (attendance, eligibility).
- Identify dimensions (who, what, where, when) and their keys. Use surrogate keys when history matters.
- Decide how history works. For each dimension attribute that can change, which SCD type? See the SCD cheatsheet.
- List measures, and say which are additive, semi-additive (balances, stock) or non-additive (ratios: store the parts and compute the ratio).
- Walk the questions back through the model. Write the SQL in your head for each question from step 1. If one needs a hack, the model is wrong.
- Cover change and failure: late-arriving facts and dimensions, corrections, deletes (GDPR), schema changes, size and partitioning.
Choosing an approach
- Kimball star schemas: the default for analytics. Easy to query, well understood by BI tools.
- Data Vault: an integration layer for many changing sources and strict audit needs, with stars built on top. See the question below for when it's worth it.
- One Big Table (OBT): a wide, pre-joined serving table. Great for BI on columnar warehouses, but built from the star, not instead of it.
- Event modeling: for clickstream and logs, keep raw events append-only and derive sessions and user-day tables.
Common traps
- Listing columns before stating the grain.
- Mixing grains in one table (an order-level fee on an order-line row gets summed many times).
- Summing a semi-additive measure over time.
- Flattening SCD2 history without saying which version each row uses.
- Many-to-many joins without a bridge table, which double count.
- Using current-state tables to build ML training data (leakage).
Practice
- Joint bank accounts and the many-to-many problem
- Model clickstream events for product analytics
- Customer address history for finance and marketing
- When would you choose Data Vault over a Kimball star schema?
- Inventory levels and semi-additive measures
- Star schema or One Big Table for the BI layer?
- Orders, partial returns and fulfillment status
- Point-in-time correct training data for a churn model
- Model trips for a ride-sharing app
- Model subscriptions so finance can report MRR movements
SQL questions that exercise modeling ideas:
- Build an SCD Type 2 table from a change log
- Revenue by the customer's country at order time vs today
- Fix the inflated order count
Further reading
- Kimball Group: dimensional modeling techniques: the original source for grain, fact types and slowly changing dimensions. Read it if you can't name the SCD types from memory.
- AWS: OLTP vs OLAP: a short primer if you need to explain why the warehouse isn't modeled like the app database.