Why dynamic tables?
In lesson 7 our daily summary needed a stream, a task and a careful MERGE, including that tricky filter for updates. It works, but every new table means more plumbing. Dynamic tables let you skip the plumbing:
| Streams + tasks (lesson 7) | Dynamic tables | |
|---|---|---|
| You write | How to update: stream, MERGE, schedule | What the table should contain: one SELECT |
| Handles updates and deletes | You do (ISUPDATE, MERGE cases) | Automatically |
| Order of refreshes in a chain | You build the task graph | Automatic, from the query dependencies |
| Freshness | Task schedule | TARGET_LAG, e.g. '1 minute' or '1 hour' |
| Best for | Procedural steps, side effects, custom logic | Most SQL transformation layers |
Build a silver and a gold layer
๐ฅraw.orders
Loaded by the pipe; may have duplicates
๐ฅanalytics.orders_clean
Dynamic table, TARGET_LAG = DOWNSTREAM
๐ฅanalytics.city_daily_revenue
Dynamic table, TARGET_LAG = 1 minute
USE SCHEMA shopkart.analytics;
-- Silver: clean orders. One row per order (latest version wins), valid rows only.
CREATE OR REPLACE DYNAMIC TABLE orders_clean
TARGET_LAG = DOWNSTREAM -- refresh only when a table built on it needs fresh data
WAREHOUSE = shopkart_wh
AS
SELECT o.order_id, o.customer_id, o.product_id, o.quantity, o.unit_price,
o.quantity * o.unit_price AS amount,
UPPER(TRIM(o.status)) AS status,
o.order_ts,
o.order_ts::DATE AS order_date
FROM shopkart.raw.orders o
WHERE o.customer_id IS NOT NULL AND o.quantity > 0
QUALIFY ROW_NUMBER() OVER (PARTITION BY o.order_id ORDER BY o.order_ts DESC) = 1;-- Gold: revenue per day, city and category, at most 1 minute behind the raw data
CREATE OR REPLACE DYNAMIC TABLE city_daily_revenue
TARGET_LAG = '1 minute'
WAREHOUSE = shopkart_wh
AS
SELECT oc.order_date, c.city, p.category,
COUNT(*) AS orders,
SUM(oc.amount) AS revenue
FROM orders_clean oc
JOIN shopkart.raw.customers c ON c.customer_id = oc.customer_id
JOIN shopkart.raw.products p ON p.product_id = oc.product_id
WHERE oc.status = 'DELIVERED'
GROUP BY 1, 2, 3;Dynamic table CITY_DAILY_REVENUE successfully created. FULL refresh mode was selected because:
This dynamic table contains a complex query. Refresh mode has been automatically set to FULL for
more predictable performance. To use INCREMENTAL, re-create the dynamic table with REFRESH_MODE=INCREMENTAL.Incremental or full refresh?
By default Snowflake chooses the refresh mode. For our gold table (two joins plus GROUP BY) it chose FULL: recompute everything each time. For 900 rows that's fine; for a billion it's expensive. We can ask for incremental refresh, and Snowflake accepts it when the query supports it:
CREATE OR REPLACE DYNAMIC TABLE city_daily_revenue
TARGET_LAG = '1 minute'
WAREHOUSE = shopkart_wh
REFRESH_MODE = INCREMENTAL -- ask for incremental refresh explicitly
AS
SELECT oc.order_date, c.city, p.category,
COUNT(*) AS orders,
SUM(oc.amount) AS revenue
FROM orders_clean oc
JOIN shopkart.raw.customers c ON c.customer_id = oc.customer_id
JOIN shopkart.raw.products p ON p.product_id = oc.product_id
WHERE oc.status = 'DELIVERED'
GROUP BY 1, 2, 3;
SHOW DYNAMIC TABLES IN SCHEMA shopkart.analytics;| name | target_lag | refresh_mode | scheduling_state | rows |
|---|---|---|---|---|
| CITY_DAILY_REVENUE | 1 minute | INCREMENTAL | ACTIVE | 898 |
| ORDERS_CLEAN | DOWNSTREAM | ADAPTIVE | ACTIVE | 5370 |
| Refresh mode | Means |
|---|---|
| INCREMENTAL | Process only rows that changed since the last refresh |
| FULL | Recompute the whole query every time |
| AUTO (default when you create it) | Snowflake picks INCREMENTAL or FULL when the table is created, and SHOW tells you which and why |
| ADAPTIVE (shown for orders_clean) | Snowflake decides at each refresh which way is cheaper |
Always check refresh_mode and refresh_mode_reason in SHOW DYNAMIC TABLES after creating one. Things like non-deterministic functions or some complex patterns force FULL.
Watch it refresh by itself
Two changes arrive: deliveries for 2 December, and the app sends one order twice.
SELECT city, SUM(orders) AS orders, SUM(revenue) AS revenue
FROM city_daily_revenue WHERE order_date = '2026-12-02' GROUP BY city ORDER BY city;
-- Deliveries come in for 2 December...
UPDATE shopkart.raw.orders SET status = 'DELIVERED'
WHERE order_ts::DATE = '2026-12-02' AND order_id <= 106040;
-- ...and the app sends one order twice (a duplicate row)
INSERT INTO shopkart.raw.orders
SELECT * FROM shopkart.raw.orders WHERE order_id = 106041;
SELECT COUNT(*) AS raw_rows_for_106041 FROM shopkart.raw.orders WHERE order_id = 106041;No data| number of rows updated |
|---|
| 15 |
| RAW_ROWS_FOR_106041 |
|---|
| 2 |
Before the change, 2 December had no delivered orders, so the gold table had no rows for that day. The raw table now has order 106041 twice. We waited about a minute and ran nothing:
-- About a minute later. We ran nothing in between.
SELECT city, SUM(orders) AS orders, SUM(revenue) AS revenue
FROM city_daily_revenue WHERE order_date = '2026-12-02' GROUP BY city ORDER BY city;
SELECT COUNT(*) AS clean_rows_for_106041 FROM orders_clean WHERE order_id = 106041;
SELECT name, state, refresh_action, refresh_trigger,
DATEDIFF('second', refresh_start_time, refresh_end_time) AS seconds
FROM TABLE(information_schema.dynamic_table_refresh_history())
WHERE name IN ('ORDERS_CLEAN', 'CITY_DAILY_REVENUE')
ORDER BY refresh_start_time DESC
LIMIT 4;| CITY | ORDERS | REVENUE |
|---|---|---|
| Delhi | 7 | 11193.00 |
| Kolkata | 2 | 4698.00 |
| Mumbai | 3 | 7897.00 |
| Pune | 3 | 5297.00 |
| CLEAN_ROWS_FOR_106041 |
|---|
| 1 |
| NAME | STATE | REFRESH_ACTION | REFRESH_TRIGGER | SECONDS |
|---|---|---|---|---|
| CITY_DAILY_REVENUE | SUCCEEDED | NO_DATA | SCHEDULED | 0 |
| ORDERS_CLEAN | SUCCEEDED | NO_DATA | SCHEDULED | 0 |
| CITY_DAILY_REVENUE | SUCCEEDED | INCREMENTAL | SCHEDULED | 2 |
| ORDERS_CLEAN | SUCCEEDED | INCREMENTAL | SCHEDULED | 1 |
Read the history from the bottom up:
- Snowflake refreshed orders_clean first (1 second), then the gold table (2 seconds), because gold depends on silver. We never told it the order.
- Both refreshes were INCREMENTAL: only the changed rows were processed.
- The duplicate never reached silver:
QUALIFYkept one row for 106041. - The next scheduled check found nothing new: NO_DATA, 0 seconds, no warehouse time.
The 15 deliveries now show up as revenue per city, at most a minute after they happened.
-- Done practising: stop the refreshes (the DOWNSTREAM table stops with it)
ALTER DYNAMIC TABLE city_daily_revenue SUSPEND;
-- Later: ALTER DYNAMIC TABLE city_daily_revenue RESUME; or refresh once by hand:
-- ALTER DYNAMIC TABLE city_daily_revenue REFRESH;How TARGET_LAG works
TARGET_LAG = '1 minute' means โthe data in this table should never be more than about a minute behind its sources.โ It's a target, not an exact schedule: Snowflake plans refreshes to meet it. Pick it by asking the business how fresh is fresh enough:
| Use | Sensible lag |
|---|---|
| Live operations dashboard | 1โ5 minutes |
| Daily management report | 1 hour, or a few hours |
| Monthly finance tables | 1 day |
| Intermediate (silver) tables | DOWNSTREAM |
Dynamic tables in a real project
| Question | Answer |
|---|---|
| Can I see the pipeline? | Yes: Snowsight shows a graph of dynamic tables and their sources, with refresh status |
| Refresh by hand? | ALTER DYNAMIC TABLE ... REFRESH |
| Pause it? | ALTER DYNAMIC TABLE ... SUSPEND / RESUME |
| Initial load | Happens on create (INITIALIZE = ON_CREATE, the default) or on the first scheduled refresh (ON_SCHEDULE) |
| Use with dbt? | Yes: dbt can create dynamic tables (materialized='dynamic_table') |
| When not to use | Non-SQL steps, calling external services, or when you need exact control of when each step runs |
๐ฏ Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1What are dynamic tables?Fresher+
Q2Dynamic tables vs streams and tasks: when do you use which?1โ3 yrs+
Q3What is TARGET_LAG = DOWNSTREAM?1โ3 yrs+
Q4Dynamic table vs materialized view?1โ3 yrs+
Q5A dynamic table is costing too much. How do you investigate?3+ yrs+
Key takeaways
- โDeclare the result and the freshness; Snowflake handles the rest.
- โOur silver and gold tables updated themselves in 1โ2 seconds, in the right order.
- โCheck the refresh mode, and pick the lag the business really needs.
- โKeep streams and tasks for the steps that aren't just a SELECT.
Frequently asked questions
What is a dynamic table in Snowflake?
A table defined by a SELECT query that Snowflake keeps up to date for you. You say how fresh it must be (TARGET_LAG); Snowflake works out when and how to refresh it, incrementally when it can, and in what order when dynamic tables depend on each other.
Dynamic table or materialized view?
Materialized views are for speeding up one query on one table: no joins, limited functions, always current. Dynamic tables are for building pipelines: joins, window functions, chains of tables, with a freshness target you choose. Use dynamic tables for ELT layers.
What does TARGET_LAG = DOWNSTREAM mean?
Refresh this table only when a dynamic table that reads from it needs fresh data. Put DOWNSTREAM on intermediate (silver) tables and a time lag on the final (gold) tables people query. Then the whole chain refreshes together at the pace the gold table needs.
Do dynamic tables cost money when nothing changes?
Very little. Snowflake checks for changes first; if the sources haven't changed, the refresh is recorded as NO_DATA and the warehouse isn't used. You pay warehouse time only for refreshes that do work. A very short lag on busy sources means frequent refreshes, so choose the lag the business really needs.
Can I insert into a dynamic table?
No. Its content always equals its query. To change the data, change the source tables or the query (CREATE OR REPLACE). That's what makes it declarative.
๐ง Quick check: dynamic tables
1. What do you declare when creating a dynamic table?
2. Best TARGET_LAG for a silver table that only feeds gold tables?
3. Snowflake picked FULL refresh mode for our gold table. Why might that matter?
4. The source didn't change. What does the next refresh do?
5. Which needs streams and tasks instead of a dynamic table?
Checking your accessโฆ