In this post we are going to cover three more shapes of INSERT in Snowflake: INSERT OVERWRITE, INSERT ALL and INSERT FIRST.
INSERT OVERWRITE empties the table before it writes. INSERT ALL and INSERT FIRST write one SELECT into several tables at once, with conditions deciding which rows go where.
Between them they are going to write seven rows from five, and five rows from the same five, and three rows from the same five again. One keyword is the difference, and the conditions never change.
We will start with the overwrite and build it up step by step. Every query below was run on a real Snowflake account, and every result you see is what it gave back.
Episode 116 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.
- Let's create some sample data
- Let's overwrite it with Tuesday's rows
- What else survived the overwrite?
- Does the default still fire on a new row?
- What happens when the OVERWRITE selects nothing?
- Now let's set up the multi-table form
- Let's start with INSERT ALL and no conditions
- Now let's add conditions that overlap on purpose
- Now let's change one keyword: ALL becomes FIRST
- What happens when there is no ELSE branch?
- Can we name the same target table twice?
- One last thing: putting the schema back
Let's create some sample data
First, where the session is pointing.
SELECT CURRENT_DATABASE() AS THE_DATABASE,
CURRENT_SCHEMA() AS THE_SCHEMA,
CURRENT_VERSION() AS THE_VERSION;| THE_DATABASE | THE_SCHEMA | THE_VERSION |
|---|---|---|
| DEMO_DB | PUBLIC | 10.35.101 |
Now a table shaped to answer a question. It has an AUTOINCREMENT counter, a column with a DEFAULT, and a comment on the table itself. We are going to overwrite this table and then see which of those three things is still there afterwards.
-- Sample data for this post.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;
-- Three things to watch survive an overwrite: the counter, the column
-- default, and the comment on the table itself.
CREATE OR REPLACE TABLE DAILY_T116 (
ROW_NO INT AUTOINCREMENT START 1 INCREMENT 1,
THE_DAY DATE,
REGION VARCHAR(20),
AMOUNT NUMBER(10,2),
LOADED VARCHAR(20) DEFAULT 'batch'
)
COMMENT = 'episode 116 probe - dropped at the end of this file';Let's load three rows for Monday, naming only the three columns we have values for.
INSERT INTO DAILY_T116 (THE_DAY, REGION, AMOUNT)
SELECT '2026-04-06'::DATE, 'north', 100.00
UNION ALL SELECT '2026-04-06'::DATE, 'south', 200.00
UNION ALL SELECT '2026-04-06'::DATE, 'east', 300.00;| number of rows inserted |
|---|
| 3 |
SELECT ROW_NO, THE_DAY, REGION, AMOUNT, LOADED FROM DAILY_T116 ORDER BY ROW_NO;| ROW_NO | THE_DAY | REGION | AMOUNT | LOADED |
|---|---|---|---|---|
| 1 | 2026-04-06 | north | 100.00 | batch |
| 2 | 2026-04-06 | south | 200.00 | batch |
| 3 | 2026-04-06 | east | 300.00 | batch |
Row numbers one, two and three, which the counter supplied. LOADED says batch on all three rows, which is the default firing.
And the catalog agrees about the table.
SELECT TABLE_NAME, ROW_COUNT, COMMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'DAILY_T116';| TABLE_NAME | ROW_COUNT | COMMENT |
|---|---|---|
| DAILY_T116 | 3 | episode 116 probe - dropped at the end of this file |
Three rows, and the comment is there.
Let's overwrite it with Tuesday's rows
INSERT OVERWRITE INTO is the same statement with one word added. Let's give it two rows for Tuesday.
INSERT OVERWRITE INTO DAILY_T116 (THE_DAY, REGION, AMOUNT)
SELECT '2026-04-07'::DATE, 'north', 111.00
UNION ALL SELECT '2026-04-07'::DATE, 'west', 222.00;| number of rows inserted |
|---|
| 2 |
Two rows inserted. Let's see what is in the table now.
SELECT ROW_NO, THE_DAY, REGION, AMOUNT, LOADED FROM DAILY_T116 ORDER BY ROW_NO;| ROW_NO | THE_DAY | REGION | AMOUNT | LOADED |
|---|---|---|---|---|
| 4 | 2026-04-07 | north | 111.00 | batch |
| 5 | 2026-04-07 | west | 222.00 | batch |
Monday is gone, and there is no DELETE and no TRUNCATE anywhere in that statement. That is what the word does.
Now look at the row numbers. They are four and five, not one and two. The counter carried straight on through the overwrite. Let's have Snowflake say that rather than read it off the grid.
SELECT MIN(ROW_NO) AS LOWEST_ROW_NO,
MAX(ROW_NO) AS HIGHEST_ROW_NO,
IFF(MIN(ROW_NO) = 1, 'RESET TO 1', 'KEPT COUNTING') AS THE_VERDICT
FROM DAILY_T116;| LOWEST_ROW_NO | HIGHEST_ROW_NO | THE_VERDICT |
|---|---|---|
| 4 | 5 | KEPT COUNTING |
KEPT COUNTING. That is the same answer TRUNCATE gave in Episode 106, asked again here on its own rather than assumed from that one.
What else survived the overwrite?
The rows were replaced. Let's check whether anything about the table itself was. The column declarations first.
SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'DAILY_T116'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | DATA_TYPE | COLUMN_DEFAULT |
|---|---|---|
| ROW_NO | NUMBER | NULL |
| THE_DAY | DATE | NULL |
| REGION | TEXT | NULL |
| AMOUNT | NUMBER | NULL |
| LOADED | TEXT | 'batch' |
Five columns, and LOADED still has its 'batch' default. The other four never had one, so COLUMN_DEFAULT is a real NULL for them.
And the comment on the table.
SELECT TABLE_NAME, COMMENT, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'DAILY_T116';| TABLE_NAME | COMMENT | ROW_COUNT |
|---|---|---|
| DAILY_T116 | episode 116 probe - dropped at the end of this file | 2 |
Still there, with the row count now reading two. So an overwrite replaces the rows. Everything that describes the table is left alone.
Does the default still fire on a new row?
A declaration that survives is not the same as a declaration that works. Let's insert one more row without naming LOADED.
INSERT INTO DAILY_T116 (THE_DAY, REGION, AMOUNT)
SELECT '2026-04-07'::DATE, 'south', 333.00;| number of rows inserted |
|---|
| 1 |
SELECT ROW_NO, REGION, AMOUNT, LOADED FROM DAILY_T116 ORDER BY ROW_NO;| ROW_NO | REGION | AMOUNT | LOADED |
|---|---|---|---|
| 4 | north | 111.00 | batch |
| 5 | west | 222.00 | batch |
| 101 | south | 333.00 | batch |
batch again, so the default is live and not just recorded.
And look at that row number. 101. The counter did not reset, and it did not carry on from five either. It is not contiguous, which is what Episode 111 showed about sequences. A counter promises you different values, not consecutive ones.
What happens when the OVERWRITE selects nothing?
Now the one that will cost you a table one day. Let's run an overwrite whose SELECT matches no rows at all.
INSERT OVERWRITE INTO DAILY_T116 (THE_DAY, REGION, AMOUNT)
SELECT '2026-04-08'::DATE, 'nowhere', 1.00 WHERE 1 = 0;| number of rows inserted |
|---|
| 0 |
number of rows inserted is 0. That reads like nothing happened. Let's count the table.
SELECT COUNT(*) AS ROWS_PRESENT FROM DAILY_T116;| ROWS_PRESENT |
|---|
| 0 |
The table is empty. Everything that was in it is gone.
INSERT OVERWRITE deletes first and inserts second, and it deletes whether the SELECT brings anything back or not. In Episode 115 a plain INSERT of zero rows changed nothing at all. The same zero here, with the opposite consequence, and the same report either way.
Now let's set up the multi-table form
The second half of the post needs a source table and somewhere to write to. Here is the source.
CREATE OR REPLACE TABLE INCOMING_T116 (
ORDER_ID INT,
REGION VARCHAR(20),
AMOUNT NUMBER(10,2)
);| status |
|---|
| Table INCOMING_T116 successfully created. |
INSERT INTO INCOMING_T116 (ORDER_ID, REGION, AMOUNT)
SELECT 1, 'north', 50.00
UNION ALL SELECT 2, 'north', 150.00
UNION ALL SELECT 3, 'south', 500.00
UNION ALL SELECT 4, 'east', 900.00
UNION ALL SELECT 5, 'west', 10.00;| number of rows inserted |
|---|
| 5 |
SELECT ORDER_ID, REGION, AMOUNT FROM INCOMING_T116 ORDER BY ORDER_ID;| ORDER_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 50.00 |
| 2 | north | 150.00 |
| 3 | south | 500.00 |
| 4 | east | 900.00 |
| 5 | west | 10.00 |
Five orders, and the amounts are chosen so that the conditions later on will overlap. Keep an eye on the two largest ones.
And three empty tables to write into.
CREATE OR REPLACE TABLE BIG_T116 (ORDER_ID INT, AMOUNT NUMBER(10,2));| status |
|---|
| Table BIG_T116 successfully created. |
CREATE OR REPLACE TABLE HUGE_T116 (ORDER_ID INT, AMOUNT NUMBER(10,2));| status |
|---|
| Table HUGE_T116 successfully created. |
CREATE OR REPLACE TABLE SMALL_T116 (ORDER_ID INT, AMOUNT NUMBER(10,2));| status |
|---|
| Table SMALL_T116 successfully created. |
Let's start with INSERT ALL and no conditions
The simplest shape is INSERT ALL, then one INTO clause per target, then the SELECT once at the end.
INSERT ALL
INTO BIG_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
INTO HUGE_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
SELECT ORDER_ID, AMOUNT FROM INCOMING_T116;| number of rows inserted into BIG_T116 | number of rows inserted into HUGE_T116 |
|---|---|
| 5 | 5 |
Have a look at the report. It has one column per target table, each naming the table it is counting. Let's check that against the tables themselves.
SELECT (SELECT COUNT(*) FROM BIG_T116) AS ROWS_IN_BIG,
(SELECT COUNT(*) FROM HUGE_T116) AS ROWS_IN_HUGE,
(SELECT COUNT(*) FROM INCOMING_T116) AS ROWS_IN_THE_SOURCE;| ROWS_IN_BIG | ROWS_IN_HUGE | ROWS_IN_THE_SOURCE |
|---|---|---|
| 5 | 5 | 5 |
Five in one, five in the other, and five in the source. So it read the source once and wrote every row to both places.
Now let's add conditions that overlap on purpose
Let's empty the two tables and start again.
TRUNCATE TABLE BIG_T116;| status |
|---|
| Statement executed successfully. |
TRUNCATE TABLE HUGE_T116;| status |
|---|
| Statement executed successfully. |
Now the same statement with a condition in front of each target. Four hundred or more goes to the huge table, one hundred or more goes to the big table, and anything else goes to the small one.
Read those conditions before the result. An amount of five hundred is over four hundred, and it is also over one hundred. It satisfies two branches.
INSERT ALL
WHEN AMOUNT >= 400 THEN INTO HUGE_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
WHEN AMOUNT >= 100 THEN INTO BIG_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
ELSE INTO SMALL_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
SELECT ORDER_ID, AMOUNT FROM INCOMING_T116;| number of rows inserted into HUGE_T116 | number of rows inserted into BIG_T116 | number of rows inserted into SMALL_T116 |
|---|---|---|
| 2 | 3 | 2 |
Three count columns this time, one per target. Let's add them up from the tables.
SELECT (SELECT COUNT(*) FROM HUGE_T116) AS IN_HUGE,
(SELECT COUNT(*) FROM BIG_T116) AS IN_BIG,
(SELECT COUNT(*) FROM SMALL_T116) AS IN_SMALL,
(SELECT COUNT(*) FROM HUGE_T116)
+ (SELECT COUNT(*) FROM BIG_T116)
+ (SELECT COUNT(*) FROM SMALL_T116) AS ROWS_WRITTEN_IN_TOTAL,
(SELECT COUNT(*) FROM INCOMING_T116) AS ROWS_READ;| IN_HUGE | IN_BIG | IN_SMALL | ROWS_WRITTEN_IN_TOTAL | ROWS_READ |
|---|---|---|---|---|
| 2 | 3 | 2 | 7 | 5 |
Seven rows written, from five rows read. Because ALL means all: every branch whose condition is true gets a copy of the row.
So which rows went to two places? Let's join the two tables and find out.
SELECT H.ORDER_ID, H.AMOUNT
FROM HUGE_T116 H
JOIN BIG_T116 B ON B.ORDER_ID = H.ORDER_ID
ORDER BY H.ORDER_ID;| ORDER_ID | AMOUNT |
|---|---|
| 3 | 500.00 |
| 4 | 900.00 |
The two orders that clear both thresholds, sitting in two tables at once.
Now let's change one keyword: ALL becomes FIRST
Empty all three tables first.
TRUNCATE TABLE HUGE_T116;| status |
|---|
| Statement executed successfully. |
TRUNCATE TABLE BIG_T116;| status |
|---|
| Statement executed successfully. |
TRUNCATE TABLE SMALL_T116;| status |
|---|
| Statement executed successfully. |
Now the identical statement with FIRST in place of ALL. The conditions are the same, in the same order, over the same data.
INSERT FIRST
WHEN AMOUNT >= 400 THEN INTO HUGE_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
WHEN AMOUNT >= 100 THEN INTO BIG_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
ELSE INTO SMALL_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
SELECT ORDER_ID, AMOUNT FROM INCOMING_T116;| number of rows inserted into HUGE_T116 | number of rows inserted into BIG_T116 | number of rows inserted into SMALL_T116 |
|---|---|---|
| 2 | 1 | 2 |
The big table got one row instead of three. Let's count them all up again.
SELECT (SELECT COUNT(*) FROM HUGE_T116) AS IN_HUGE,
(SELECT COUNT(*) FROM BIG_T116) AS IN_BIG,
(SELECT COUNT(*) FROM SMALL_T116) AS IN_SMALL,
(SELECT COUNT(*) FROM HUGE_T116)
+ (SELECT COUNT(*) FROM BIG_T116)
+ (SELECT COUNT(*) FROM SMALL_T116) AS ROWS_WRITTEN_IN_TOTAL,
(SELECT COUNT(*) FROM INCOMING_T116) AS ROWS_READ;| IN_HUGE | IN_BIG | IN_SMALL | ROWS_WRITTEN_IN_TOTAL | ROWS_READ |
|---|---|---|---|---|
| 2 | 1 | 2 | 5 | 5 |
Five rows written, from five rows read. And nothing should be in two places now.
SELECT COUNT(*) AS ORDERS_IN_BOTH_TABLES
FROM HUGE_T116 H JOIN BIG_T116 B ON B.ORDER_ID = H.ORDER_ID;| ORDERS_IN_BOTH_TABLES |
|---|
| 0 |
Nothing is. FIRST stops at the first branch whose condition is true and moves on to the next row. So the keyword is the logic. The conditions never changed and neither did the data.
What happens when there is no ELSE branch?
This is the one to remember. Empty the three tables once more.
TRUNCATE TABLE HUGE_T116;| status |
|---|
| Statement executed successfully. |
TRUNCATE TABLE BIG_T116;| status |
|---|
| Statement executed successfully. |
TRUNCATE TABLE SMALL_T116;| status |
|---|
| Statement executed successfully. |
Now the same INSERT FIRST with the ELSE branch taken away. Four hundred or more to the huge table, one hundred or more to the big table, and that is the whole statement.
INSERT FIRST
WHEN AMOUNT >= 400 THEN INTO HUGE_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
WHEN AMOUNT >= 100 THEN INTO BIG_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
SELECT ORDER_ID, AMOUNT FROM INCOMING_T116;| number of rows inserted into HUGE_T116 | number of rows inserted into BIG_T116 |
|---|---|
| 2 | 1 |
Two counts, and they add up to three rows written. Five were read. Let's have Snowflake do that subtraction.
SELECT (SELECT COUNT(*) FROM INCOMING_T116) AS ROWS_READ,
(SELECT COUNT(*) FROM HUGE_T116)
+ (SELECT COUNT(*) FROM BIG_T116) AS ROWS_WRITTEN,
(SELECT COUNT(*) FROM INCOMING_T116)
- ((SELECT COUNT(*) FROM HUGE_T116)
+ (SELECT COUNT(*) FROM BIG_T116)) AS ROWS_THAT_VANISHED;| ROWS_READ | ROWS_WRITTEN | ROWS_THAT_VANISHED |
|---|---|---|
| 5 | 3 | 2 |
Two rows vanished. No error, no count, and nothing anywhere in that report says anything went missing.
Let's find out which ones.
SELECT ORDER_ID, AMOUNT FROM INCOMING_T116
WHERE ORDER_ID NOT IN (SELECT ORDER_ID FROM HUGE_T116)
AND ORDER_ID NOT IN (SELECT ORDER_ID FROM BIG_T116)
ORDER BY ORDER_ID;| ORDER_ID | AMOUNT |
|---|---|
| 1 | 50.00 |
| 5 | 10.00 |
The two orders that were under both thresholds, so they matched no branch and there was nothing left to catch them.
Put an ELSE on it, even if the ELSE only writes to a reject table. Then the arithmetic adds up, and you can go and look at what fell out instead of wondering whether anything did.
Can we name the same target table twice?
Last one. Two branches, both writing into the big table: one for a hundred or more, one for four hundred or more.
INSERT ALL
WHEN AMOUNT >= 100 THEN INTO BIG_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
WHEN AMOUNT >= 400 THEN INTO BIG_T116 (ORDER_ID, AMOUNT) VALUES (ORDER_ID, AMOUNT)
SELECT ORDER_ID, AMOUNT FROM INCOMING_T116;| number of rows inserted |
|---|
| 5 |
It is allowed. And have a look at the report: one column called number of rows inserted, with no table name on it. When both branches name the same table, the per-target columns we saw earlier collapse into a single count.
Let's group the big table by order and count the copies.
SELECT ORDER_ID, COUNT(*) AS COPIES
FROM BIG_T116 GROUP BY ORDER_ID ORDER BY ORDER_ID;| ORDER_ID | COPIES |
|---|---|
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
Three orders, two copies each. So yes, a target named twice is written twice. Whether that is useful or a bug is your call, and Snowflake is not going to ask.
Be careful reading that last grid, because the table was not emptied before the statement ran. The big table already held the one row the ELSE-less INSERT FIRST left in it in the section above. This statement's first branch matched three orders and its second branch matched two, which is the five rows it reports. So it wrote two of those orders twice. The third one shows two copies because one of them was already there.
One last thing: putting the schema back
Five tables were made for this post, so five tables go.
DROP TABLE IF EXISTS DAILY_T116;| status |
|---|
| DAILY_T116 successfully dropped. |
DROP TABLE IF EXISTS INCOMING_T116;| status |
|---|
| INCOMING_T116 successfully dropped. |
DROP TABLE IF EXISTS BIG_T116;| status |
|---|
| BIG_T116 successfully dropped. |
DROP TABLE IF EXISTS HUGE_T116;| status |
|---|
| HUGE_T116 successfully dropped. |
DROP TABLE IF EXISTS SMALL_T116;| status |
|---|
| SMALL_T116 successfully dropped. |
SELECT CURRENT_DATABASE() AS THE_DATABASE,
CURRENT_SCHEMA() AS THE_SCHEMA,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
WHERE RIGHT(TABLE_NAME, 5) = '_T116') AS PROBE_TABLES_LEFT,
(SELECT COUNT(*) FROM CUSTOMERS) AS DEMO_DB_IS_READABLE;| THE_DATABASE | THE_SCHEMA | PROBE_TABLES_LEFT | DEMO_DB_IS_READABLE |
|---|---|---|---|
| DEMO_DB | PUBLIC | 0 | 10 |
No probe tables left, and the rest of the database still reads fine.
Key points
INSERT OVERWRITEreplaces the rows and nothing else. TheAUTOINCREMENTcounter kept counting, the column default stayed, and the table comment stayed.- An
INSERT OVERWRITEwhoseSELECTreturns nothing still empties the table. It deletes first and inserts second. - A plain
INSERTof zero rows is harmless. AnINSERT OVERWRITEof zero rows is not, and the report looks the same. - An unconditional
INSERT ALLreads the source once and writes every row to every target. - The report carries one count column per target table, named after the table — unless two branches name the same table, in which case there is one count for the statement.
INSERT ALLwrites a copy into every branch whose condition is true, so five rows in can be seven rows out.INSERT FIRSTstops at the first branch that matches. Identical conditions, identical data, five rows out.- With no
ELSEbranch, a row that matches nothing is dropped silently. No error and no count. - The same target table may be named in two branches, and is written twice.
Episode 117 — UPDATE. Every UPDATE in Snowflake reports two numbers, and almost nobody reads the second one. It is the one that tells you your join matched the same row twice.
Every query and every result grid on this page was captured from a live Snowflake account (version 10.35.101) running the demo script for this episode. The five _T116 tables are created and dropped by the script and the orders in them are made up for teaching. Cells reading NULL are real nulls from INFORMATION_SCHEMA. Nothing on this page is timed and no performance claims are made anywhere in this series. Multi-table INSERT behaviour follows the Snowflake multi-table INSERT documentation.



No comments:
Post a Comment
Note: Only a member of this blog may post a comment.