Data modelling · core
Slowly changing dimensions
A dimension attribute can change. SCD type 1 overwrites, type 2 keeps history as new rows, type 3 keeps one previous value. The choice decides whether old reports change.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Type 1 overwrites: no history. It suits corrections and attributes whose history nobody needs. Type 2 adds a row per change with a surrogate key, the natural key, valid_from, valid_to (or 9999-12-31) and is_current. Type 3 adds a previous-value column.
- Type 0 never changes. Type 4 uses a separate history or mini-dimension. Type 6 combines 1, 2 and 3: history rows plus the current value on every row.
- Fact lookup: store the surrogate key of the version current at load time, or join on the natural key with the event time between valid_from and valid_to with one end exclusive, so a row never matches two versions.
- Type 2 merge: clean staging to one row per key (latest by updated_at). Step 1 closes changed current rows. Step 2 inserts new versions for changed and new keys. Use a null-safe comparison (IS DISTINCT FROM, or <=> in Spark).
- A name-only change can be a type 1 update on the current row. Say so and confirm with the interviewer.
- One MERGE trick: union the staging rows twice, once with the real key to close or update and once with a NULL merge key to force the insert of the new version. Or run two steps in one transaction.
- Make it safe to rerun: the load date comes from the run, not CURRENT_DATE, and a second run finds the current row equal to staging so nothing changes.
- Deletes: close the current row and flag it deleted. dbt snapshots do this with hard_deletes: invalidate or new_record (dbt 1.9 and later).
- Old values cannot come back from a type 1 table. If last year's revenue for a city changes overnight, a bulk update on a type 1 attribute is the first thing to check. Store the order's city on the fact, or make the attribute type 2.
The red pen
Common traps: mistake → fix
Mistake: Make every column type 2
Fix: row counts multiply; choose per attribute and write it down.
Mistake: Inclusive BETWEEN on both valid_from and valid_to is fine
Fix: an order on the boundary date matches two versions.
Mistake: Plain = in the change check is enough
Fix: it misses changes to or from NULL.
Easy to hard
Questions interviewers ask, easy to hard
- How do SCD types 1, 2 and 3 handle a customer moving from Pune to Bengaluru?
- Write the SQL steps for an SCD type 2 load.
- How do you join facts to a type 2 dimension without matching two versions?
- Last year's revenue for Pune dropped 6% overnight and nobody touched the fact table. What do you check?
- How do you make an SCD load safe to rerun?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.