Snowflake SQL Tutorial 116 - INSERT ALL Wrote 7 Rows From 5. INSERT FIRST Wrote 5

Snowflake SQL · Episode 116

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

First, where the session is pointing.

Query
SELECT CURRENT_DATABASE()  AS THE_DATABASE,
       CURRENT_SCHEMA()    AS THE_SCHEMA,
       CURRENT_VERSION()   AS THE_VERSION;
Result1 row · 3 columns
THE_DATABASETHE_SCHEMATHE_VERSION
DEMO_DBPUBLIC10.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
-- 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.

Query
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;
Result1 row · 1 column
number of rows inserted
3
Query
SELECT ROW_NO, THE_DAY, REGION, AMOUNT, LOADED FROM DAILY_T116 ORDER BY ROW_NO;
Result3 rows · 5 columns
ROW_NOTHE_DAYREGIONAMOUNTLOADED
12026-04-06north100.00batch
22026-04-06south200.00batch
32026-04-06east300.00batch

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.

Query
SELECT TABLE_NAME, ROW_COUNT, COMMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'DAILY_T116';
Result1 row · 3 columns
TABLE_NAMEROW_COUNTCOMMENT
DAILY_T1163episode 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.

Query
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;
Result1 row · 1 column
number of rows inserted
2

Two rows inserted. Let's see what is in the table now.

Query
SELECT ROW_NO, THE_DAY, REGION, AMOUNT, LOADED FROM DAILY_T116 ORDER BY ROW_NO;
Result2 rows · 5 columns
ROW_NOTHE_DAYREGIONAMOUNTLOADED
42026-04-07north111.00batch
52026-04-07west222.00batch

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.

Query
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;
Result1 row · 3 columns
LOWEST_ROW_NOHIGHEST_ROW_NOTHE_VERDICT
45KEPT 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.

Query
SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'DAILY_T116'
ORDER BY ORDINAL_POSITION;
Result5 rows · 3 columns
COLUMN_NAMEDATA_TYPECOLUMN_DEFAULT
ROW_NONUMBERNULL
THE_DAYDATENULL
REGIONTEXTNULL
AMOUNTNUMBERNULL
LOADEDTEXT'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.

Query
SELECT TABLE_NAME, COMMENT, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'DAILY_T116';
Result1 row · 3 columns
TABLE_NAMECOMMENTROW_COUNT
DAILY_T116episode 116 probe - dropped at the end of this file2

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.

Query
INSERT INTO DAILY_T116 (THE_DAY, REGION, AMOUNT)
SELECT '2026-04-07'::DATE, 'south', 333.00;
Result1 row · 1 column
number of rows inserted
1
Query
SELECT ROW_NO, REGION, AMOUNT, LOADED FROM DAILY_T116 ORDER BY ROW_NO;
Result3 rows · 4 columns
ROW_NOREGIONAMOUNTLOADED
4north111.00batch
5west222.00batch
101south333.00batch

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.

Query
INSERT OVERWRITE INTO DAILY_T116 (THE_DAY, REGION, AMOUNT)
SELECT '2026-04-08'::DATE, 'nowhere', 1.00 WHERE 1 = 0;
Result1 row · 1 column
number of rows inserted
0

number of rows inserted is 0. That reads like nothing happened. Let's count the table.

Query
SELECT COUNT(*) AS ROWS_PRESENT FROM DAILY_T116;
Result1 row · 1 column
ROWS_PRESENT
0

The table is empty. Everything that was in it is gone.

The same zero, the opposite meaning

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.

Query
CREATE OR REPLACE TABLE INCOMING_T116 (
    ORDER_ID INT,
    REGION   VARCHAR(20),
    AMOUNT   NUMBER(10,2)
);
Result1 row · 1 column
status
Table INCOMING_T116 successfully created.
Query
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;
Result1 row · 1 column
number of rows inserted
5
Query
SELECT ORDER_ID, REGION, AMOUNT FROM INCOMING_T116 ORDER BY ORDER_ID;
Result5 rows · 3 columns
ORDER_IDREGIONAMOUNT
1north50.00
2north150.00
3south500.00
4east900.00
5west10.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.

Query
CREATE OR REPLACE TABLE BIG_T116   (ORDER_ID INT, AMOUNT NUMBER(10,2));
Result1 row · 1 column
status
Table BIG_T116 successfully created.
Query
CREATE OR REPLACE TABLE HUGE_T116  (ORDER_ID INT, AMOUNT NUMBER(10,2));
Result1 row · 1 column
status
Table HUGE_T116 successfully created.
Query
CREATE OR REPLACE TABLE SMALL_T116 (ORDER_ID INT, AMOUNT NUMBER(10,2));
Result1 row · 1 column
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.

Query
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;
Result1 row · 2 columns
number of rows inserted into BIG_T116number of rows inserted into HUGE_T116
55

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.

Query
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;
Result1 row · 3 columns
ROWS_IN_BIGROWS_IN_HUGEROWS_IN_THE_SOURCE
555

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.

Query
TRUNCATE TABLE BIG_T116;
Result1 row · 1 column
status
Statement executed successfully.
Query
TRUNCATE TABLE HUGE_T116;
Result1 row · 1 column
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.

Query
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;
Result1 row · 3 columns
number of rows inserted into HUGE_T116number of rows inserted into BIG_T116number of rows inserted into SMALL_T116
232

Three count columns this time, one per target. Let's add them up from the tables.

Query
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;
Result1 row · 5 columns
IN_HUGEIN_BIGIN_SMALLROWS_WRITTEN_IN_TOTALROWS_READ
23275

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.

Query
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;
Result2 rows · 2 columns
ORDER_IDAMOUNT
3500.00
4900.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.

Query
TRUNCATE TABLE HUGE_T116;
Result1 row · 1 column
status
Statement executed successfully.
Query
TRUNCATE TABLE BIG_T116;
Result1 row · 1 column
status
Statement executed successfully.
Query
TRUNCATE TABLE SMALL_T116;
Result1 row · 1 column
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.

Query
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;
Result1 row · 3 columns
number of rows inserted into HUGE_T116number of rows inserted into BIG_T116number of rows inserted into SMALL_T116
212

The big table got one row instead of three. Let's count them all up again.

Query
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;
Result1 row · 5 columns
IN_HUGEIN_BIGIN_SMALLROWS_WRITTEN_IN_TOTALROWS_READ
21255

Five rows written, from five rows read. And nothing should be in two places now.

Query
SELECT COUNT(*) AS ORDERS_IN_BOTH_TABLES
FROM HUGE_T116 H JOIN BIG_T116 B ON B.ORDER_ID = H.ORDER_ID;
Result1 row · 1 column
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.

Query
TRUNCATE TABLE HUGE_T116;
Result1 row · 1 column
status
Statement executed successfully.
Query
TRUNCATE TABLE BIG_T116;
Result1 row · 1 column
status
Statement executed successfully.
Query
TRUNCATE TABLE SMALL_T116;
Result1 row · 1 column
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.

Query
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;
Result1 row · 2 columns
number of rows inserted into HUGE_T116number of rows inserted into BIG_T116
21

Two counts, and they add up to three rows written. Five were read. Let's have Snowflake do that subtraction.

Query
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;
Result1 row · 3 columns
ROWS_READROWS_WRITTENROWS_THAT_VANISHED
532

Two rows vanished. No error, no count, and nothing anywhere in that report says anything went missing.

Let's find out which ones.

Query
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;
Result2 rows · 2 columns
ORDER_IDAMOUNT
150.00
510.00

The two orders that were under both thresholds, so they matched no branch and there was nothing left to catch them.

Always write the ELSE

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.

Query
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;
Result1 row · 1 column
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.

Query
SELECT ORDER_ID, COUNT(*) AS COPIES
FROM BIG_T116 GROUP BY ORDER_ID ORDER BY ORDER_ID;
Result3 rows · 2 columns
ORDER_IDCOPIES
22
32
42

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.

Two of those three came from this statement

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.

Query
DROP TABLE IF EXISTS DAILY_T116;
Result1 row · 1 column
status
DAILY_T116 successfully dropped.
Query
DROP TABLE IF EXISTS INCOMING_T116;
Result1 row · 1 column
status
INCOMING_T116 successfully dropped.
Query
DROP TABLE IF EXISTS BIG_T116;
Result1 row · 1 column
status
BIG_T116 successfully dropped.
Query
DROP TABLE IF EXISTS HUGE_T116;
Result1 row · 1 column
status
HUGE_T116 successfully dropped.
Query
DROP TABLE IF EXISTS SMALL_T116;
Result1 row · 1 column
status
SMALL_T116 successfully dropped.
Query
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;
Result1 row · 4 columns
THE_DATABASETHE_SCHEMAPROBE_TABLES_LEFTDEMO_DB_IS_READABLE
DEMO_DBPUBLIC010

No probe tables left, and the rest of the database still reads fine.

Key points

  • INSERT OVERWRITE replaces the rows and nothing else. The AUTOINCREMENT counter kept counting, the column default stayed, and the table comment stayed.
  • An INSERT OVERWRITE whose SELECT returns nothing still empties the table. It deletes first and inserts second.
  • A plain INSERT of zero rows is harmless. An INSERT OVERWRITE of zero rows is not, and the report looks the same.
  • An unconditional INSERT ALL reads 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 ALL writes a copy into every branch whose condition is true, so five rows in can be seven rows out.
  • INSERT FIRST stops at the first branch that matches. Identical conditions, identical data, five rows out.
  • With no ELSE branch, 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.
Next in this series

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.