In this post we are going to cover full load vs incremental load, and what change data capture (CDC) is.
These are the three common ways to move data from a source system into the warehouse. A full load copies everything every time. An incremental load copies only the rows that changed since the last run, usually by tracking a timestamp called a watermark. CDC reads the source database's transaction log and picks up every insert, update and delete in order.
We will run a watermark-based incremental load in SQL, find the one kind of change it can never see, and then fix it with CDC records.
Question 37 of the Real DWH Question and Answers series on TechBrothersIT.
- The interview answer
- Full load: truncate and reload
- Let's create some sample data
- Step 1: Incremental extract with a watermark
- Step 2: Apply the changes and move the watermark
- Step 3: The blind spot - hard deletes
- Step 4: Apply the deletes from CDC
- Side by side: which one when
- Follow-up questions interviewers ask
- The full script
The interview answer
A full load truncates the target and reloads everything: simple, but slow and costly as data grows. An incremental load moves only rows changed since the last run, usually with a watermark on a timestamp column, but it cannot see hard deletes. Change data capture (CDC) reads the source's transaction log, and captures inserts, updates and deletes in order.
Full load: truncate and reload
Let's start with the simplest one. Every run, we empty the target table and copy the whole source again. Inserts, updates and deletes are all handled automatically, because we simply rebuild everything.
- Good for small tables - a list of countries or product categories reloads in no time.
- Bad for big tables - copying 100 million orders every night is slow, puts heavy load on the source system, and costs money.
That is where incremental loads come in.
Let's create some sample data
We need four tables: the source src_orders as it is today, the warehouse copy dw_orders as of the last load, an etl_watermark table that remembers where the last load stopped, and cdc_orders, which holds what a CDC tool would read from the source's transaction log.
DROP TABLE IF EXISTS cdc_orders;
DROP TABLE IF EXISTS dw_orders;
DROP TABLE IF EXISTS etl_watermark;
DROP TABLE IF EXISTS src_orders;
-- the SOURCE system today. Since the last load: order 3 was updated,
-- order 4 is new, and order 2 was deleted.
CREATE TABLE src_orders (order_id INT PRIMARY KEY, status VARCHAR(20), amount DECIMAL(10,2), updated_at TIMESTAMP);
INSERT INTO src_orders VALUES
(1, 'Delivered', 20.00, '2026-05-30 08:00'),
(3, 'Shipped', 45.00, '2026-06-01 09:00'),
(4, 'New', 15.00, '2026-06-01 10:00');
-- the WAREHOUSE copy, as of the last load
CREATE TABLE dw_orders (order_id INT PRIMARY KEY, status VARCHAR(20), amount DECIMAL(10,2), updated_at TIMESTAMP);
INSERT INTO dw_orders VALUES
(1, 'Delivered', 20.00, '2026-05-30 08:00'),
(2, 'New', 30.00, '2026-05-31 12:00'),
(3, 'New', 45.00, '2026-05-31 15:00');
-- the high-water mark: the latest updated_at we have already loaded
CREATE TABLE etl_watermark (table_name VARCHAR(30), last_loaded_at TIMESTAMP);
INSERT INTO etl_watermark VALUES ('orders', '2026-05-31 15:00');
-- what a CDC tool reads from the source's transaction log
CREATE TABLE cdc_orders (lsn INT, op CHAR(1), order_id INT, status VARCHAR(20), amount DECIMAL(10,2));
INSERT INTO cdc_orders VALUES
(101, 'D', 2, NULL, NULL),
(102, 'U', 3, 'Shipped', 45.00),
(103, 'I', 4, 'New', 15.00);Here is the source system today.
SELECT * FROM src_orders ORDER BY 1;| order_id | status | amount | updated_at |
|---|---|---|---|
| 1 | Delivered | 20.00 | 2026-05-30 08:00 |
| 3 | Shipped | 45.00 | 2026-06-01 09:00 |
| 4 | New | 15.00 | 2026-06-01 10:00 |
Three orders. Since the last load, order 3 was updated to Shipped, order 4 is new, and order 2 was deleted, which is why it is not here.
Now the warehouse copy from the last load.
SELECT * FROM dw_orders ORDER BY 1;| order_id | status | amount | updated_at |
|---|---|---|---|
| 1 | Delivered | 20.00 | 2026-05-30 08:00 |
| 2 | New | 30.00 | 2026-05-31 12:00 |
| 3 | New | 45.00 | 2026-05-31 15:00 |
The warehouse still has orders 1, 2 and 3, with order 3 still in status New.
SELECT * FROM etl_watermark ORDER BY 1;| table_name | last_loaded_at |
|---|---|
| orders | 2026-05-31 15:00 |
The watermark is the latest updated_at we have already loaded: 2026-05-31 15:00.
Step 1: Incremental extract with a watermark
Now let's extract only the rows whose updated_at is later than the watermark.
SELECT order_id, status, amount, updated_at
FROM src_orders
WHERE updated_at > (SELECT last_loaded_at
FROM etl_watermark
WHERE table_name = 'orders')
ORDER BY order_id;| order_id | status | amount | updated_at |
|---|---|---|---|
| 3 | Shipped | 45.00 | 2026-06-01 09:00 |
| 4 | New | 15.00 | 2026-06-01 10:00 |
Only two rows travel instead of the whole table: order 3, updated at 2026-06-01 09:00, and order 4, new at 2026-06-01 10:00. Order 1 has not changed since the last load, so it stays behind. On a large table, this is the difference between hours and seconds.
Step 2: Apply the changes and move the watermark
Now let's apply those changes to the warehouse. We delete the changed rows from the target and insert their new versions. This delete plus insert pattern works in any database, and a MERGE does the same job. Then we move the watermark to the newest updated_at in the source, so the next run starts from there.
DELETE FROM dw_orders
WHERE order_id IN (SELECT order_id FROM src_orders
WHERE updated_at > TIMESTAMP '2026-05-31 15:00');
INSERT INTO dw_orders
SELECT * FROM src_orders
WHERE updated_at > TIMESTAMP '2026-05-31 15:00';
UPDATE etl_watermark
SET last_loaded_at = (SELECT MAX(updated_at) FROM src_orders)
WHERE table_name = 'orders';Now let's check the result.
SELECT * FROM dw_orders ORDER BY order_id;| order_id | status | amount | updated_at |
|---|---|---|---|
| 1 | Delivered | 20.00 | 2026-05-30 08:00 |
| 2 | New | 30.00 | 2026-05-31 12:00 |
| 3 | Shipped | 45.00 | 2026-06-01 09:00 |
| 4 | New | 15.00 | 2026-06-01 10:00 |
Order 3 is now Shipped and order 4 is loaded. The last statement moved the watermark to 2026-06-01 10:00, the newest updated_at in the source. But look at order 2: it is still in the warehouse with status New.
Step 3: The blind spot - hard deletes
Let's compare the warehouse keys with the source keys to see what the incremental load missed.
SELECT order_id FROM dw_orders
EXCEPT
SELECT order_id FROM src_orders;| order_id |
|---|
| 2 |
Order 2 is in the warehouse but no longer in the source. It was hard deleted. The incremental load asks for rows with a newer updated_at, but a deleted row does not exist anymore, so it has no updated_at. The watermark can never see it.
The usual fixes:
- Soft deletes - the source keeps the row and sets a deleted flag instead of removing it.
- A periodic full key compare - like the query above, run on a schedule.
- Change data capture - read the deletes straight from the log.
Step 4: Apply the deletes from CDC
Every database writes each change to its transaction log. A CDC tool, like Debezium or the built-in CDC in SQL Server, reads that log and gives us every change in order, with its operation: I for insert, U for update, D for delete. Here are the records in cdc_orders.
SELECT * FROM cdc_orders ORDER BY 1;| lsn | op | order_id | status | amount |
|---|---|---|---|---|
| 101 | D | 2 | NULL | NULL |
| 102 | U | 3 | Shipped | 45.00 |
| 103 | I | 4 | New | 15.00 |
LSN 101 is the delete of order 2, 102 is the update to order 3 and 103 is the insert of order 4. The update and insert are already in the warehouse from Step 2, so let's apply the delete.
DELETE FROM dw_orders
WHERE order_id IN (SELECT order_id FROM cdc_orders
WHERE op = 'D');Now let's check the result.
SELECT * FROM dw_orders ORDER BY order_id;| order_id | status | amount | updated_at |
|---|---|---|---|
| 1 | Delivered | 20.00 | 2026-05-30 08:00 |
| 3 | Shipped | 45.00 | 2026-06-01 09:00 |
| 4 | New | 15.00 | 2026-06-01 10:00 |
Order 2 is gone. The warehouse now holds orders 1, 3 and 4, exactly like the source. CDC is also near real time, and it barely touches the source tables because it reads the log, not the tables.
Side by side: which one when
| Method | Speed | Hard deletes | Use for |
|---|---|---|---|
| Full load | slow | ✓ yes | small tables |
| Watermark | fast | ✗ no | soft-delete sources |
| CDC (log) | fast, near real time | ✓ yes | large, busy tables |
- Full load - simplest and catches everything, but slow on big tables. Use it for small dimensions and reference data.
- Watermark incremental - fast and easy, but needs a reliable
updated_atcolumn and misses hard deletes. Use it when the source only soft deletes. - CDC - complete and near real time, but more infrastructure to run. Use it for large, busy tables where deletes matter.
Follow-up questions interviewers ask
Why can a watermark load miss changes?
Hard deletes have no updated_at, and a source that does not update the timestamp reliably hides changes too.
How do you catch deletes without CDC?
Soft deletes in the source, or a periodic full comparison of the keys.
Name some CDC tools.
Debezium, SQL Server CDC, Oracle GoldenGate, AWS DMS, Fivetran.
The full script
Here is everything from this post in one script. Copy it and run it top to bottom.
-- ========================================================================
-- Data Warehouse Interview Q&A #37 - Full vs incremental load, and CDC
-- 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_q37; SET search_path TO dwh_q37;
-- 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 cdc_orders;
DROP TABLE IF EXISTS dw_orders;
DROP TABLE IF EXISTS etl_watermark;
DROP TABLE IF EXISTS src_orders;
-- the SOURCE system today. Since the last load: order 3 was updated,
-- order 4 is new, and order 2 was deleted.
CREATE TABLE src_orders (order_id INT PRIMARY KEY, status VARCHAR(20), amount DECIMAL(10,2), updated_at TIMESTAMP);
INSERT INTO src_orders VALUES
(1, 'Delivered', 20.00, '2026-05-30 08:00'),
(3, 'Shipped', 45.00, '2026-06-01 09:00'),
(4, 'New', 15.00, '2026-06-01 10:00');
-- the WAREHOUSE copy, as of the last load
CREATE TABLE dw_orders (order_id INT PRIMARY KEY, status VARCHAR(20), amount DECIMAL(10,2), updated_at TIMESTAMP);
INSERT INTO dw_orders VALUES
(1, 'Delivered', 20.00, '2026-05-30 08:00'),
(2, 'New', 30.00, '2026-05-31 12:00'),
(3, 'New', 45.00, '2026-05-31 15:00');
-- the high-water mark: the latest updated_at we have already loaded
CREATE TABLE etl_watermark (table_name VARCHAR(30), last_loaded_at TIMESTAMP);
INSERT INTO etl_watermark VALUES ('orders', '2026-05-31 15:00');
-- what a CDC tool reads from the source's transaction log
CREATE TABLE cdc_orders (lsn INT, op CHAR(1), order_id INT, status VARCHAR(20), amount DECIMAL(10,2));
INSERT INTO cdc_orders VALUES
(101, 'D', 2, NULL, NULL),
(102, 'U', 3, 'Shipped', 45.00),
(103, 'I', 4, 'New', 15.00);
-- Step 1: Incremental extract: only rows changed after the watermark
SELECT order_id, status, amount, updated_at
FROM src_orders
WHERE updated_at > (SELECT last_loaded_at
FROM etl_watermark
WHERE table_name = 'orders')
ORDER BY order_id;
-- Step 2: Apply the changes (delete + insert), then move the watermark
DELETE FROM dw_orders
WHERE order_id IN (SELECT order_id FROM src_orders
WHERE updated_at > TIMESTAMP '2026-05-31 15:00');
INSERT INTO dw_orders
SELECT * FROM src_orders
WHERE updated_at > TIMESTAMP '2026-05-31 15:00';
UPDATE etl_watermark
SET last_loaded_at = (SELECT MAX(updated_at) FROM src_orders)
WHERE table_name = 'orders';
-- check the result
SELECT * FROM dw_orders ORDER BY order_id;
-- Step 3: The blind spot: rows deleted in the source are still in the warehouse
SELECT order_id FROM dw_orders
EXCEPT
SELECT order_id FROM src_orders;
-- Step 4: Apply the CDC deletes
DELETE FROM dw_orders
WHERE order_id IN (SELECT order_id FROM cdc_orders
WHERE op = 'D');
-- check the result
SELECT * FROM dw_orders ORDER BY order_id;
Key points
- Full load: truncate and reload everything
- Incremental: rows after the watermark
- Watermarks miss hard deletes
- CDC reads the log: I, U and D
- Pick by table size, deletes and latency
Question 38 — Staging layer and medallion architecture What is a staging area? What are bronze, silver and gold layers?
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.