DWH Interview Question 35 - What are SCD Type 4 and SCD Type 6?

Data Warehouse Interview Q&A · Question 35

In this post we are going to cover SCD Type 4 and SCD Type 6, the two slowly changing dimension types that come up right after Types 0 to 3.

Type 4 keeps only the current values in the dimension and moves the old values to a separate history table. Type 6 combines Types 1, 2 and 3 in one table: Type 2 rows with a historical column, plus a current column that is overwritten on every version. That lets us report sales as they happened or as things are today from the same table.

We will build both with the same customer move we used for the earlier SCD types, and then run two reports on Type 6 to see why it is useful.

Question 35 of the Real DWH Question and Answers series on TechBrothersIT.

The interview answer

Say this first

SCD Type 4 keeps the current values in the dimension and moves old values to a separate history table; in Kimball's terms, Type 4 is the mini-dimension. SCD Type 6 combines 1 + 2 + 3: Type 2 rows with a historical column, plus a current column overwritten on every version, so you can report as it was or as it is now from one table.

Let's create some sample data

We need a Type 4 dimension with its empty history table, a Type 6 dimension with two city columns, and a small fact table with two February sales that point to the Type 6 keys.

Sample data — run this first
DROP TABLE IF EXISTS fact_sales;
DROP TABLE IF EXISTS customer_history;
DROP TABLE IF EXISTS dim_customer_t4;
DROP TABLE IF EXISTS dim_customer_t6;

-- TYPE 4: current values in the dimension, old values in a separate history table
CREATE TABLE dim_customer_t4 (customer_key INT PRIMARY KEY, customer_id VARCHAR(10), city VARCHAR(50), valid_from DATE);
INSERT INTO dim_customer_t4 VALUES (1, 'C101', 'Toronto', '2025-01-01'), (2, 'C102', 'Dallas', '2025-01-01');
CREATE TABLE customer_history (customer_id VARCHAR(10), city VARCHAR(50), valid_from DATE, valid_to DATE);

-- TYPE 6: Type 2 rows + a current_city column overwritten on every version
CREATE TABLE dim_customer_t6 (
  customer_key    INT PRIMARY KEY,
  customer_id     VARCHAR(10),
  historical_city VARCHAR(50),   -- Type 2: true when the row was valid
  current_city    VARCHAR(50),   -- Type 1: always the latest value
  valid_from      DATE,
  valid_to        DATE,
  is_current      CHAR(1)
);
INSERT INTO dim_customer_t6 VALUES
  (1, 'C101', 'Toronto', 'Toronto', '2025-01-01', '9999-12-31', 'Y'),
  (2, 'C102', 'Dallas',  'Dallas',  '2025-01-01', '9999-12-31', 'Y');

CREATE TABLE fact_sales (sale_date DATE, customer_key INT, amount DECIMAL(10,2));
INSERT INTO fact_sales VALUES ('2026-02-10', 1, 80.00), ('2026-02-15', 2, 40.00);

The Type 4 dimension holds one row per customer with today's values only.

Query
SELECT * FROM dim_customer_t4 ORDER BY 1;
Result2 rows · 4 columns
customer_keycustomer_idcityvalid_from
1C101Toronto2025-01-01
2C102Dallas2025-01-01

The Type 6 dimension looks like a Type 2 table, but it has two city columns: historical_city and current_city. Right now they hold the same values.

Query
SELECT * FROM dim_customer_t6 ORDER BY 1;
Result2 rows · 7 columns
customer_keycustomer_idhistorical_citycurrent_cityvalid_fromvalid_tois_current
1C101TorontoToronto2025-01-019999-12-31Y
2C102DallasDallas2025-01-019999-12-31Y

And the two sales: 80.00 for customer key 1 (Sara, C101) and 40.00 for key 2 (Omar, C102).

Query
SELECT * FROM fact_sales ORDER BY 1;
Result2 rows · 3 columns
sale_datecustomer_keyamount
2026-02-10180.00
2026-02-15240.00

Step 1: Type 4 - move the old value to a history table

Sara, customer C101, moves from Toronto to Vancouver on 2026-06-01. With Type 4, we first copy her current row into customer_history with an end date, then overwrite the dimension.

Statement
INSERT INTO customer_history
SELECT customer_id, city, valid_from, DATE '2026-05-31'
FROM dim_customer_t4
WHERE customer_id = 'C101';

UPDATE dim_customer_t4
SET city = 'Vancouver', valid_from = DATE '2026-06-01'
WHERE customer_id = 'C101';

Now let's check the result.

Query
SELECT * FROM dim_customer_t4 ORDER BY customer_key;
Result2 rows · 4 columns
customer_keycustomer_idcityvalid_from
1C101Vancouver2026-06-01
2C102Dallas2025-01-01

The dimension now says Vancouver, valid from 2026-06-01, and still has one row per customer. It stays small and fast. The Toronto value is not lost: the first statement copied it into customer_history with a valid_to of 2026-05-31, ready for audit or the occasional history question.

The catch: facts point to the dimension, which only knows today's city. To report sales by the city at the time of the sale, we have to join the history table by date. That is why Type 2 is more common in star schemas.

Type 4 has two definitions

This is a detail worth knowing for the interview. Type 4 means two different things depending on who you ask.

  • History table - current values in the dimension, old rows in a separate table. This is what many teams and tools mean, and what we just built.
  • Mini-dimension - in Kimball's book, Type 4 is the mini-dimension: fast-changing attributes like age band or loyalty tier split into their own small dimension, with their own key on the fact. That was question 28 in this series.
Tip

In the interview, name both definitions and ask which one they mean. It shows you know the material, not just one version of it.

Step 2: Type 6 - a Type 2 change plus a current column

Now Type 6. The name comes from 1 + 2 + 3 = 6. When Sara moves, we do a normal Type 2 change: expire row 1 and insert row 3 for Vancouver. Then we overwrite current_city on all of her rows.

Statement
UPDATE dim_customer_t6
SET valid_to = DATE '2026-05-31', is_current = 'N'
WHERE customer_id = 'C101' AND is_current = 'Y';

INSERT INTO dim_customer_t6 VALUES (3, 'C101', 'Vancouver', 'Vancouver',
  DATE '2026-06-01', DATE '9999-12-31', 'Y');

UPDATE dim_customer_t6
SET current_city = 'Vancouver'
WHERE customer_id = 'C101';

Now let's check the result.

Query
SELECT * FROM dim_customer_t6 ORDER BY customer_key;
Result3 rows · 7 columns
customer_keycustomer_idhistorical_citycurrent_cityvalid_fromvalid_tois_current
1C101TorontoVancouver2025-01-012026-05-31N
2C102DallasDallas2025-01-019999-12-31Y
3C101VancouverVancouver2026-06-019999-12-31Y

Look at row 1. historical_city still says Toronto, because that was true while the row was valid (until 2026-05-31). But current_city now says Vancouver. Row 3 is the new current version with Vancouver in both columns.

  • historical_city is the Type 2 part: what was true at the time.
  • current_city is the Type 1 part: always the latest value.
  • Having the old and the new value side by side on the same row is the Type 3 part.

Step 3: A new sale in June

Now let's add a June sale for Sara. A normal fact load looks up the current row, so it gets key 3.

Statement
INSERT INTO fact_sales VALUES ('2026-06-10', 3, 50.00);

Now let's check the result.

Query
SELECT * FROM fact_sales ORDER BY sale_date;
Result3 rows · 3 columns
sale_datecustomer_keyamount
2026-02-10180.00
2026-02-15240.00
2026-06-10350.00

Sara's February sale points to row 1 and her June sale points to row 3. The fact table now holds three sales.

Step 4: Report by the city at the time of the sale

Let's group sales by historical_city.

Query
SELECT d.historical_city, SUM(f.amount) AS sales
FROM fact_sales f
JOIN dim_customer_t6 d ON d.customer_key = f.customer_key
GROUP BY d.historical_city
ORDER BY sales DESC;
Result3 rows · 2 columns
historical_citysales
Toronto80.00
Vancouver50.00
Dallas40.00

Toronto keeps the February sale of 80.00, Vancouver gets the June sale of 50.00, and Dallas has 40.00. This is sales as they happened.

Step 5: Report by today's city

Now the same join, grouped by current_city instead.

Query
SELECT d.current_city, SUM(f.amount) AS sales
FROM fact_sales f
JOIN dim_customer_t6 d ON d.customer_key = f.customer_key
GROUP BY d.current_city
ORDER BY sales DESC;
Result2 rows · 2 columns
current_citysales
Vancouver130.00
Dallas40.00

All of Sara's sales roll up to Vancouver, 130.00, as if she had always lived there, and Dallas still has 40.00. This view is useful for comparing regions under today's structure.

Same fact table, same join, just a different column. No self-join and no extra query.

All SCD types side by side

TypeHow it worksHistory
0retain the originalnone
1overwritenone
2new row + new keyfull
3previous-value columnone level
4history table / mini-dimensionfull, kept aside
61 + 2 + 3: historical + current columnfull + current

Zero retains, one overwrites, two adds a row and three adds a column. Four is the history table, or in Kimball's terms the mini-dimension, and six combines one, two and three. Types 5 and 7 also exist as further combinations. In practice most warehouses use Types 1 and 2, and Type 6 when the business wants both views.

Follow-up questions interviewers ask

Why is Type 4 less common in star schemas?

Facts point to the dimension that holds only current values, so historical reporting needs a date join to the history table.

Why is it called Type 6?

It combines Types 1, 2 and 3: 1 + 2 + 3 = 6.

How do you load current_city in Type 6?

After the Type 2 change, overwrite current_city on every row with the same natural key.

The full script

Here is everything from this post in one script. Copy it and run it top to bottom.

Full script — copy and run top to bottom
-- ========================================================================
-- Data Warehouse Interview Q&A #35 - SCD Types 4 and 6
-- TechBrothersIT. Sample data is invented for teaching.
-- Standard SQL, tested on PostgreSQL 16. Runs top to bottom in one go.
-- Run it in an empty database or schema: other videos reuse table names
-- like dim_customer, and their leftover tables can block the DROPs below.
-- PostgreSQL example:  CREATE SCHEMA dwh_q35;  SET search_path TO dwh_q35;
-- SQL Server: write DATE '2026-06-01' as CAST('2026-06-01' AS DATE).
-- ========================================================================

-- Step 0: create and load the sample data
DROP TABLE IF EXISTS fact_sales;
DROP TABLE IF EXISTS customer_history;
DROP TABLE IF EXISTS dim_customer_t4;
DROP TABLE IF EXISTS dim_customer_t6;

-- TYPE 4: current values in the dimension, old values in a separate history table
CREATE TABLE dim_customer_t4 (customer_key INT PRIMARY KEY, customer_id VARCHAR(10), city VARCHAR(50), valid_from DATE);
INSERT INTO dim_customer_t4 VALUES (1, 'C101', 'Toronto', '2025-01-01'), (2, 'C102', 'Dallas', '2025-01-01');
CREATE TABLE customer_history (customer_id VARCHAR(10), city VARCHAR(50), valid_from DATE, valid_to DATE);

-- TYPE 6: Type 2 rows + a current_city column overwritten on every version
CREATE TABLE dim_customer_t6 (
  customer_key    INT PRIMARY KEY,
  customer_id     VARCHAR(10),
  historical_city VARCHAR(50),   -- Type 2: true when the row was valid
  current_city    VARCHAR(50),   -- Type 1: always the latest value
  valid_from      DATE,
  valid_to        DATE,
  is_current      CHAR(1)
);
INSERT INTO dim_customer_t6 VALUES
  (1, 'C101', 'Toronto', 'Toronto', '2025-01-01', '9999-12-31', 'Y'),
  (2, 'C102', 'Dallas',  'Dallas',  '2025-01-01', '9999-12-31', 'Y');

CREATE TABLE fact_sales (sale_date DATE, customer_key INT, amount DECIMAL(10,2));
INSERT INTO fact_sales VALUES ('2026-02-10', 1, 80.00), ('2026-02-15', 2, 40.00);


-- Step 1: Type 4: copy the old value to the history table, then overwrite the dimension
INSERT INTO customer_history
SELECT customer_id, city, valid_from, DATE '2026-05-31'
FROM dim_customer_t4
WHERE customer_id = 'C101';

UPDATE dim_customer_t4
SET city = 'Vancouver', valid_from = DATE '2026-06-01'
WHERE customer_id = 'C101';

-- check the result
SELECT * FROM dim_customer_t4 ORDER BY customer_key;

-- Step 2: Type 6: a Type 2 change, plus overwrite current_city on ALL versions
UPDATE dim_customer_t6
SET valid_to = DATE '2026-05-31', is_current = 'N'
WHERE customer_id = 'C101' AND is_current = 'Y';

INSERT INTO dim_customer_t6 VALUES (3, 'C101', 'Vancouver', 'Vancouver',
  DATE '2026-06-01', DATE '9999-12-31', 'Y');

UPDATE dim_customer_t6
SET current_city = 'Vancouver'
WHERE customer_id = 'C101';

-- check the result
SELECT * FROM dim_customer_t6 ORDER BY customer_key;

-- Step 3: A new sale in June points to the new version
INSERT INTO fact_sales VALUES ('2026-06-10', 3, 50.00);

-- check the result
SELECT * FROM fact_sales ORDER BY sale_date;

-- Step 4: Report by the city at the time of the sale
SELECT d.historical_city, SUM(f.amount) AS sales
FROM fact_sales f
JOIN dim_customer_t6 d ON d.customer_key = f.customer_key
GROUP BY d.historical_city
ORDER BY sales DESC;

-- Step 5: Report by the customer's city today
SELECT d.current_city, SUM(f.amount) AS sales
FROM fact_sales f
JOIN dim_customer_t6 d ON d.customer_key = f.customer_key
GROUP BY d.current_city
ORDER BY sales DESC;

Key points

  • Type 4: current in the dim, old values in a history table
  • Kimball's Type 4 = the mini-dimension
  • Type 6 = 1 + 2 + 3
  • historical_city (Type 2) + current_city (Type 1)
  • Report as it was or as it is, no self-join
Next in this series

Question 36 — Load dimensions or facts first? In what order do you load dimensions and facts, and why?

Every query and every result on this page was run on PostgreSQL 16 using the script above. The SQL is standard and runs on most databases with small changes (noted in the script). All tables and data are invented demo data for teaching.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.