Skip to content
Silver 8 min read · All formats 2 practice problems ↓

Slowly changing dimensions (SCD Type 2)

A customer moves city. Do you overwrite the old address, or keep both and record when each was true? Slowly changing dimensions are the standard answers, and Type 2, keeping history, is the one interviews ask you to build.

Step 3 · Data lake & lakehouse · Silver · Changing data, lesson 2 of 3

Read first: MERGE INTO and upserts · eqNullSafe (<=>) (skip if you know them)

In plain words first

A passport records your current address, but a court case needs to know where you lived then. Type 2 keeps every address with the dates it was valid, so each fact joins to the address it had at the time.

Types 1, 2 and 3

TypeOn changeHistoryTypical use
Type 1Overwrite the attributeNoneCorrections, attributes nobody reports on historically
Type 2Close the current row, insert a new versionFullCustomer segment, address, price: anything facts must be joined to as it was
Type 3Keep the previous value in an extra columnOne step backRare: "previous region" after a reorganisation

Type 2 matters because facts must join to the dimension as it was when the fact happened. A March order from a customer who moved in May belongs to the old city in a March report.

The Type 2 columns

ColumnPurpose
customer_idThe business key. Not unique any more: one row per version.
customer_sk (optional)A surrogate key unique per version, which facts can store directly.
valid_from, valid_toWhen this version was true. The current row has valid_to null or a far-future date such as 9999-12-31.
is_currentA convenience flag so "current state" queries need no date logic.
row_hash (optional)A hash of the tracked attributes, to detect changes with one comparison.

One MERGE, two actions

For a changed customer, the batch must do two things to the target: update the current row (close it) and insert the new version. One MERGE clause can only do one of those per source row. The standard trick stagesstage: A group of steps Spark can run without moving data between machines. A new stage starts at every shuffle. Learn more → every changed row twice:

  1. 1
    Copy 1 keeps the real key as merge_key. It matches the current target row, and the MATCHED clause closes it.
  2. 2
    Copy 2 has merge_key set to null, so it can never match. The NOT MATCHED clause inserts it as the new current version.
  3. 3
    Brand-new customers only need copy 1: they do not match anything and are inserted.
PySparkSpark SQL · SCD Type 2 with a staged union
# Step 1: stage the changes. Rows that are new or differ from the current version,
# each changed one twice.
spark.sql("""
    CREATE OR REPLACE TEMP VIEW staged AS
    WITH changes AS (
      SELECT s.*
      FROM updates s
      LEFT JOIN customers t ON t.customer_id = s.customer_id AND t.is_current
      WHERE t.customer_id IS NULL                  -- new customer
         OR NOT (t.city <=> s.city)                -- changed attribute (null-safe)
    )
    SELECT customer_id AS merge_key, * FROM changes    -- copy 1: closes the old row
    UNION ALL
    SELECT NULL AS merge_key, * FROM changes c         -- copy 2: inserts the new row
    WHERE EXISTS (SELECT 1 FROM customers t
                  WHERE t.customer_id = c.customer_id AND t.is_current)
""")

# Step 2: one MERGE closes the old versions and inserts the new ones.
spark.sql("""
    MERGE INTO customers t
    USING staged s
    ON t.customer_id = s.merge_key AND t.is_current
    WHEN MATCHED THEN UPDATE SET is_current = false, valid_to = s.changed_at
    WHEN NOT MATCHED THEN INSERT (customer_id, city, valid_from, valid_to, is_current)
      VALUES (s.customer_id, s.city, s.changed_at, NULL, true)
""")
-- Step 1: stage the changes. Rows that are new or differ from the current version,
-- each changed one twice.
CREATE OR REPLACE TEMP VIEW staged AS
WITH changes AS (
  SELECT s.*
  FROM updates s
  LEFT JOIN customers t ON t.customer_id = s.customer_id AND t.is_current
  WHERE t.customer_id IS NULL                  -- new customer
     OR NOT (t.city <=> s.city)                -- changed attribute (null-safe)
)
SELECT customer_id AS merge_key, * FROM changes    -- copy 1: closes the old row
UNION ALL
SELECT NULL AS merge_key, * FROM changes c         -- copy 2: inserts the new row
WHERE EXISTS (SELECT 1 FROM customers t
              WHERE t.customer_id = c.customer_id AND t.is_current);

-- Step 2: one MERGE closes the old versions and inserts the new ones.
MERGE INTO customers t
USING staged s
ON t.customer_id = s.merge_key AND t.is_current
WHEN MATCHED THEN UPDATE SET is_current = false, valid_to = s.changed_at
WHEN NOT MATCHED THEN INSERT (customer_id, city, valid_from, valid_to, is_current)
  VALUES (s.customer_id, s.city, s.changed_at, NULL, true)

The staging step reads the target table, so it is a separate view rather than a WITH clause in front of the MERGE: open-source Delta (4.4 on Spark 4.2) rejects a MERGE whose own CTE reads the MERGE target ("Table does not support reads"). A CTE that only reads the source is fine.

Only changed rows are staged, so unchanged customers are not rewritten. With many tracked columns, compare a hash of them instead: t.row_hash <> s.row_hash, computed with null-safe concatenation (for example sha2(concat_ws('|', coalesce(city, '∅'), ...), 256)).

Querying history

PySparkSpark SQL · Join facts to the version that was valid at the time
spark.sql("""
    SELECT o.order_id, o.order_ts, c.city
    FROM orders o
    JOIN customers c
      ON c.customer_id = o.customer_id
     AND o.order_ts >= c.valid_from
     AND (c.valid_to IS NULL OR o.order_ts < c.valid_to)
""")
SELECT o.order_id, o.order_ts, c.city
FROM orders o
JOIN customers c
  ON c.customer_id = o.customer_id
 AND o.order_ts >= c.valid_from
 AND (c.valid_to IS NULL OR o.order_ts < c.valid_to)

Use a half-open interval (>= start, < end) so a fact exactly at a change time matches exactly one version. This is a range join: if it is slow, storing the surrogate key on the fact at load time avoids it.

Edge cases

  • Several changes for one key in one batch — The MERGE fails on duplicate matches, or keeps only one change. Either keep the latest per key (losing intermediate history), or apply changes in sequence order, one version per change, by building the versions with lead(changed_at) before merging.
  • Late, out-of-order changes — A change dated before the current version's start must be inserted in the middle of history, splitting a version. The simple MERGE assumes changes arrive in order; handle late ones separately or rebuild the key's history.
  • Re-running a batch — Without the change check, a rerun closes and reinserts every row again. The "differs from current" filter makes reruns no-ops.
  • Null attributes — t.city != s.city is null when either side is null, so a change to or from null is missed. Use <=> or compare hashes.
  • Deletes — Decide what a source delete means: close the current row with no new version, or insert a version flagged as deleted.
Note: managed pipelines can do this for you: Databricks declarative pipelines (formerly Delta Live Tables) have APPLY CHANGES (now AUTO CDC) with STORED AS SCD TYPE 2, which also handles out-of-order changes by a sequence column. Knowing the manual MERGE is still what interviews test.

Try it yourself

Each example says what it shows, then the output from a real run and what to notice in it, so you can follow without running anything. To try one yourself, paste it into a local Spark session with Delta Lake, with spark already created: each builds its own data.

Goal: Keep the history of a changing attribute (SCD type 2) with one MERGE, safe to re-run.

Production patternPySpark · The lesson's staged-union MERGE, run twice: history kept, re-runs are no-ops
import shutil

spark.sql("DROP TABLE IF EXISTS customers")
# leftovers from a new session
shutil.rmtree("spark-warehouse/customers", ignore_errors=True)
spark.sql("""CREATE TABLE customers (customer_id INT, city STRING, valid_from DATE,
                                     valid_to DATE, is_current BOOLEAN) USING delta""")
spark.sql("""INSERT INTO customers VALUES
             (1, 'Pune', DATE'2025-01-01', NULL, true),
             (2, 'Delhi', DATE'2025-01-01', NULL, true)""")

(spark.createDataFrame(
    [(1, "Mumbai", "2025-03-01"), (2, "Delhi", "2025-03-01"), (3, "Goa", "2025-03-01")],
    ["customer_id", "city", "changed_at"])
    .selectExpr("customer_id", "city", "CAST(changed_at AS DATE) AS changed_at")
    .createOrReplaceTempView("updates"))

# Step 1: stage the changes. (Delta cannot read the MERGE target
# inside the MERGE's own CTE, so this is a separate view.)
stage = """
CREATE OR REPLACE TEMP VIEW staged AS
WITH changes AS (            -- rows that are new or differ from the current version
  SELECT s.*
  FROM updates s
  LEFT JOIN customers t ON t.customer_id = s.customer_id AND t.is_current
  WHERE t.customer_id IS NULL                 -- new customer
     OR NOT (t.city <=> s.city)               -- changed attribute (null-safe)
)
SELECT customer_id AS merge_key, * FROM changes        -- copy 1: closes the old row
UNION ALL
SELECT NULL AS merge_key, * FROM changes c               -- copy 2: inserts the new row
WHERE EXISTS (SELECT 1 FROM customers t
              WHERE t.customer_id = c.customer_id AND t.is_current)"""

# Step 2: one MERGE closes the old versions and inserts the new ones.
merge = """
MERGE INTO customers t
USING staged s
ON t.customer_id = s.merge_key AND t.is_current
WHEN MATCHED THEN UPDATE SET is_current = false, valid_to = s.changed_at
WHEN NOT MATCHED THEN INSERT (customer_id, city, valid_from, valid_to, is_current)
  VALUES (s.customer_id, s.city, s.changed_at, NULL, true)"""

for run in range(2):          # the same batch twice: the second run finds no changes
    spark.sql(stage)
    spark.sql(merge)
spark.table("customers").orderBy("customer_id", "valid_from").show()

Output from a real run

+-----------+------+----------+----------+----------+
|customer_id|  city|valid_from|  valid_to|is_current|
+-----------+------+----------+----------+----------+
|          1|  Pune|2025-01-01|2025-03-01|     false|
|          1|Mumbai|2025-03-01|      NULL|      true|
|          2| Delhi|2025-01-01|      NULL|      true|
|          3|   Goa|2025-03-01|      NULL|      true|
+-----------+------+----------+----------+----------+

What to notice:

  • Customer 1 moved from Pune to Mumbai: the Pune row was closed (valid_to set, is_current false) and a Mumbai row added.
  • Customer 2's city did not change, so its row was left alone. Customer 3 is new and got one row.
  • The batch ran twice, and the second run changed nothing.

Common mistakes

  • Updating in place and calling it Type 2 — Without closing and inserting, history is lost.
  • Comparing attributes with != — Changes to or from null are missed.
  • Closed intervals for valid_from / valid_to — A fact at the boundary matches two versions.

Key takeaways

  • Type 1 overwrites, Type 2 keeps every version, Type 3 keeps one previous value.
  • Type 2 needs valid_from, valid_to and usually is_current per version.
  • Stage changed rows twice so one MERGE can close the old version and insert the new one.
  • Compare null-safely, handle multiple and late changes, and join facts with a half-open interval.

Interview prep

THE QUESTION

"Implement an SCD Type 2 customer dimension on Delta, loaded from a daily change batch."

Columns: business key, tracked attributes, valid_from, valid_to (null for current), is_current, optionally a surrogate key and a row hash. Each run: find rows that are new or differ from the current version (null-safe or by hash), stage changed rows twice (once with the real key to close the current row, once with a null merge key to insert the new version), and MERGE: matched closes, not matched inserts. Mention deduplicating or sequencing multiple changes per key, late changes, null-safe comparison, idempotent reruns, and the half-open interval join for facts.

Avoid: "add an updated_at column and overwrite the row". That is Type 1 with a timestamp; the old version is gone.

Follow-ups · answer out loud, then open

Follow-up"Why use a surrogate key in a Type 2 dimension?"
The business key is no longer unique, since there is one row per version. A surrogate key identifies one version, so facts can store it at load time and join with a simple equality instead of a date-range join, which is faster and unambiguous.
Scenario"A batch contains three changes for the same customer on the same day. What do you do?"
Decide whether intermediate versions matter. If not, keep the latest per key before merging. If they do, build all versions first: order changes by sequence, set each valid_to with lead(changed_at), then close the current target row once and insert all new versions, with only the last one current.
Trap"Detect changes with t.address != s.address."
A change from null to a value, or back, gives null, not true, so it is missed. Use null-safe comparison (NOT (t.address <=> s.address)) or compare hashes built with explicit null markers.

Check yourself

3 questions

Why is each changed row staged twice?

Show the answer

One copy closes the current version (matched), the other inserts the new one (not matched). A single source row can only trigger one MERGE action.

A fact at exactly the change time must match one version. Which interval do you use?

Show the answer

valid_from <= ts < valid_to. A half-open interval assigns boundary instants to exactly one version.

The source city changes from null to "Pune", and the change is not picked up. Why?

Show the answer

null != "Pune" is null, not true. Use <=> or compare hashes.

Practise this

Problems that use this, graded on hidden tests

Keep going

Up next · Changing data · lesson 3 of 3 · 5 min read
Change Data Feed
Read the row-level inserts, updates and deletes made to a Delta table between two versions.

Related lessons

Previous: MERGE INTO and upserts

Primary sources: Delta Lake: SCD Type 2 with merge · MERGE INTO (Delta)