In this post we are going to cover the INSERT statement in Snowflake.
INSERT puts rows into a table. It is the first statement most people learn, and it has one behaviour that will quietly put your data in the wrong columns: the column list is matched by position, never by name.
We will start with the three ways of writing it, then look at what a column you leave out gets, then at what INSERT will convert for you and what it refuses to convert. Then we will write the one line that ruins a table, and look at why nothing warned us about it.
Every query below was run on a real Snowflake account, and every result and every error you see is what it gave back.
Episode 115 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.
- Let's create some sample data
- Let's start with the three ways of writing it
- What does a column we leave out get?
- Can we ask for the default by name?
- Now let's drop the column list entirely
- What happens when the count is wrong?
- Now let's see what INSERT converts for us
- Where does it refuse to convert?
- Now the one line that ruins a table
- Let's write the same statement the right way round
- What happens when the SELECT matches nothing?
- Can a table insert into itself?
- 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 the table we are going to insert into. It has five columns, and one of them matters later: CITY has a DEFAULT of the word unknown. JOINED has no default at all.
-- Sample data for this post.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;
CREATE OR REPLACE TABLE PEOPLE_T115 (
PERSON_ID INT,
FIRST_NAME VARCHAR(30),
LAST_NAME VARCHAR(30),
CITY VARCHAR(30) DEFAULT 'unknown',
JOINED DATE
);That is an empty table. Let's start putting rows in it.
Let's start with the three ways of writing it
The first spelling is one row. Name the columns, then VALUES, then one set of values.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, LAST_NAME, CITY, JOINED)
VALUES (1, 'Ana', 'Alvarez', 'Lisbon', '2026-01-10'::DATE);| number of rows inserted |
|---|
| 1 |
number of rows inserted is 1. That is the whole report Snowflake gives you.
The second spelling is many rows. The same column list, then several sets of values separated by commas.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, LAST_NAME, CITY, JOINED)
VALUES (2, 'Ben', 'Brandt', 'Berlin', '2026-01-22'::DATE),
(3, 'Cleo', 'Costa', 'Porto', '2026-02-03'::DATE),
(4, 'Dan', 'Dahl', 'Oslo', '2026-02-15'::DATE);| number of rows inserted |
|---|
| 3 |
One statement, three rows. This is the one to use when you are typing in a handful of rows by hand.
The third spelling is the one you write at work. Instead of VALUES, a SELECT.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, LAST_NAME, CITY, JOINED)
SELECT 5, 'Eve', 'Eriksen', 'Bergen', '2026-03-01'::DATE;| number of rows inserted |
|---|
| 1 |
Let's look at the table.
SELECT PERSON_ID, FIRST_NAME, LAST_NAME, CITY, JOINED
FROM PEOPLE_T115 ORDER BY PERSON_ID;| PERSON_ID | FIRST_NAME | LAST_NAME | CITY | JOINED |
|---|---|---|---|---|
| 1 | Ana | Alvarez | Lisbon | 2026-01-10 |
| 2 | Ben | Brandt | Berlin | 2026-01-22 |
| 3 | Cleo | Costa | Porto | 2026-02-03 |
| 4 | Dan | Dahl | Oslo | 2026-02-15 |
| 5 | Eve | Eriksen | Bergen | 2026-03-01 |
Five rows. Ana, Ben, Cleo, Dan and Eve, and every column is filled in because we named every column every time.
What does a column we leave out get?
Now let's name only two of the five columns and see what happens to the other three.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES (6, 'Finn');| number of rows inserted |
|---|
| 1 |
One row inserted, same as before. Let's go and look at it.
SELECT PERSON_ID, FIRST_NAME, LAST_NAME, CITY, JOINED
FROM PEOPLE_T115 WHERE PERSON_ID = 6;| PERSON_ID | FIRST_NAME | LAST_NAME | CITY | JOINED |
|---|---|---|---|---|
| 6 | Finn | NULL | unknown | NULL |
Finn arrived with three columns we never mentioned. LAST_NAME is NULL and JOINED is NULL, because neither of those has a default.
But CITY says unknown. That is the DEFAULT we declared on the table doing its job.
Let's count that rather than eyeball it, so you are not taking my word for it.
SELECT COUNT(*) AS ROWS_TOTAL,
COUNT(LAST_NAME) AS ROWS_WITH_A_LAST_NAME,
COUNT(CITY) AS ROWS_WITH_A_CITY,
SUM(IFF(CITY = 'unknown', 1, 0)) AS ROWS_THAT_TOOK_THE_DEFAULT
FROM PEOPLE_T115;| ROWS_TOTAL | ROWS_WITH_A_LAST_NAME | ROWS_WITH_A_CITY | ROWS_THAT_TOOK_THE_DEFAULT |
|---|---|---|---|
| 6 | 5 | 6 | 1 |
Six rows in the table. Five of them have a last name, so one does not. All six have a city, and exactly one of those cities came from the default.
The video says “leave two columns out” here. Naming PERSON_ID and FIRST_NAME out of five columns leaves three out, and the grid above shows all three of them: LAST_NAME, CITY and JOINED. Two of them came back null and the third took its default.
Can we ask for the default by name?
Yes. DEFAULT is a keyword you can write inside the values list, in the position of the column you want it for.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, CITY)
VALUES (7, 'Gus', DEFAULT);| number of rows inserted |
|---|
| 1 |
SELECT PERSON_ID, FIRST_NAME, CITY FROM PEOPLE_T115 WHERE PERSON_ID = 7;| PERSON_ID | FIRST_NAME | CITY |
|---|---|---|
| 7 | Gus | unknown |
unknown again. So you can let a column fall through to its default by leaving it out, or you can say DEFAULT out loud. Saying it out loud is clearer to whoever reads the statement next.
Now let's drop the column list entirely
You are allowed to leave the column list off. INSERT INTO, the table name, then VALUES. That means every column, in the order the table declares them.
INSERT INTO PEOPLE_T115
VALUES (8, 'Hana', 'Haas', 'Vienna', '2026-03-20'::DATE);| number of rows inserted |
|---|
| 1 |
It works. And it works because I happened to get all five values right, in the table's own order. Nothing in that statement says what any of those values is for.
What happens when the count is wrong?
That form has no column list to check the values against, so let's find out what Snowflake does check. First, too few values.
INSERT INTO PEOPLE_T115
VALUES (9, 'Iris', 'Ibsen');002020 (21S01): SQL compilation error: Insert value list does not match column list expecting 5 but got 3
Refused. Insert value list does not match column list expecting 5 but got 3. Clear, and impossible to misread.
And too many.
INSERT INTO PEOPLE_T115
VALUES (10, 'Jon', 'Jansen', 'Malmo', '2026-04-01'::DATE, 'extra');002020 (21S01): SQL compilation error: Insert value list does not match column list expecting 5 but got 6
Refused the same way, with the numbers the other way round.
Let's make sure neither of those half-landed.
SELECT COUNT(*) AS ROWS_PRESENT FROM PEOPLE_T115;| ROWS_PRESENT |
|---|
| 8 |
Eight rows, which is exactly what was there before both of those statements. A refused INSERT leaves the table alone.
So the count is checked. Snowflake can do that much arithmetic. What it cannot check is whether the values are in the right order, because nothing on the server knows what you meant by them. Hold on to that for two more sections.
Now let's see what INSERT converts for us
There is one more thing to know before the bug. INSERT converts values to fit the column, and it does it without saying anything.
PERSON_ID is an INT. Let's hand it a string of digits.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES ('11', 'Kai');| number of rows inserted |
|---|
| 1 |
One row inserted. No complaint. Now let's check whether what landed is really a number, by adding one to it.
SELECT PERSON_ID, FIRST_NAME, PERSON_ID + 1 AS IT_IS_A_NUMBER_IF_THIS_WORKS
FROM PEOPLE_T115 WHERE FIRST_NAME = 'Kai';| PERSON_ID | FIRST_NAME | IT_IS_A_NUMBER_IF_THIS_WORKS |
|---|---|---|
| 11 | Kai | 12 |
It added. So the quotes were dropped and the value was stored as a number.
Now the other direction. FIRST_NAME is a VARCHAR. Let's hand it a number.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES (12, 99999);| number of rows inserted |
|---|
| 1 |
SELECT PERSON_ID, FIRST_NAME, LENGTH(FIRST_NAME) AS LENGTH_AS_TEXT
FROM PEOPLE_T115 WHERE PERSON_ID = 12;| PERSON_ID | FIRST_NAME | LENGTH_AS_TEXT |
|---|---|---|
| 12 | 99999 | 5 |
LENGTH works on it and reports five characters, so that is real text now. It converts in both directions, and it said nothing either time.
Where does it refuse to convert?
Only where the value genuinely does not fit. Let's put a word into the integer column.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES ('not a number', 'Lena');100038 (22018): DML operation to table PEOPLE_T115 failed on column PERSON_ID with error: Numeric value 'not a number' is not recognized
Refused. Numeric value ‘not a number’ is not recognized, and it names the column it failed on.
And a first name longer than the thirty characters the column allows.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES (13, 'a first name far longer than thirty characters');100078 (22000): DML operation to table PEOPLE_T115 failed on column FIRST_NAME with error: String 'a first name far longer than thirty characters' is too long and would be truncated
Refused too. String ... is too long and would be truncated. It will not quietly cut the end off.
So INSERT will convert, but it will not lose anything doing it. That is a good rule, and it is about to be our problem.
Now the one line that ruins a table
Here is a second table, standing in for an import from somewhere else. Look at the order of its two columns: the surname comes first and the given name second, which is the opposite way round to our table.
CREATE OR REPLACE TABLE IMPORT_T115 (
SURNAME VARCHAR(30),
GIVEN_NAME VARCHAR(30)
);| status |
|---|
| Table IMPORT_T115 successfully created. |
INSERT INTO IMPORT_T115 (SURNAME, GIVEN_NAME)
VALUES ('Moreau', 'Marie'),
('Nowak', 'Nina');| number of rows inserted |
|---|
| 2 |
SELECT SURNAME, GIVEN_NAME FROM IMPORT_T115 ORDER BY SURNAME;| SURNAME | GIVEN_NAME |
|---|---|
| Moreau | Marie |
| Nowak | Nina |
Two rows. Moreau, Marie and Nowak, Nina.
Now the insert, and I want you to read this one before you look at the result.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, LAST_NAME)
SELECT 20, SURNAME, GIVEN_NAME FROM IMPORT_T115 WHERE SURNAME = 'Moreau';| number of rows inserted |
|---|
| 1 |
One row inserted, and nothing else. No warning of any kind.
Those three target column names are correct. PERSON_ID, FIRST_NAME, LAST_NAME, spelled right, in the right table. Here is what landed.
SELECT PERSON_ID, FIRST_NAME, LAST_NAME
FROM PEOPLE_T115 WHERE PERSON_ID = 20;| PERSON_ID | FIRST_NAME | LAST_NAME |
|---|---|---|
| 20 | Moreau | Marie |
FIRST_NAME holds Moreau, which is the surname. LAST_NAME holds Marie, which is the given name. The row is wrong and nothing anywhere said so.
The column list was right. The SELECT was in the source's order. INSERT matched them by position, first to first, second to second, third to third. Both of those columns are VARCHAR, so the conversion check from the last section had nothing to object to.
Let's write the same statement the right way round
The fix is two words swapped in the SELECT. Nothing in the column list changes.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, LAST_NAME)
SELECT 21, GIVEN_NAME, SURNAME FROM IMPORT_T115 WHERE SURNAME = 'Nowak';| number of rows inserted |
|---|
| 1 |
SELECT PERSON_ID, FIRST_NAME, LAST_NAME
FROM PEOPLE_T115 WHERE PERSON_ID IN (20, 21) ORDER BY PERSON_ID;| PERSON_ID | FIRST_NAME | LAST_NAME |
|---|---|---|
| 20 | Moreau | Marie |
| 21 | Nina | Nowak |
Both rows from the import, side by side. The first one has the surname in the first name column. The second one is right. Same statement, one word order apart.
And here is one row that counts the damage, which is the query to run against your own table if you think this may have happened to you.
SELECT SUM(IFF(FIRST_NAME = 'Moreau', 1, 0)) AS SURNAMES_IN_THE_FIRST_NAME_COLUMN,
SUM(IFF(FIRST_NAME = 'Nina', 1, 0)) AS GIVEN_NAMES_IN_THE_FIRST_NAME_COLUMN,
COUNT(*) AS ROWS_FROM_THE_IMPORT
FROM PEOPLE_T115 WHERE PERSON_ID IN (20, 21);| SURNAMES_IN_THE_FIRST_NAME_COLUMN | GIVEN_NAMES_IN_THE_FIRST_NAME_COLUMN | ROWS_FROM_THE_IMPORT |
|---|---|---|
| 1 | 1 | 2 |
Read the SELECT, not the column list. The column list tells you what the writer intended. The SELECT is what decides where each value actually goes.
What happens when the SELECT matches nothing?
Two more, quickly, because both of them come up. First an INSERT whose SELECT returns no rows at all.
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
SELECT 99, 'Nobody' WHERE 1 = 0;| number of rows inserted |
|---|
| 0 |
number of rows inserted is 0. Not an error. Zero is a legal answer, which is worth knowing, because a script will sail straight past it and report success.
SELECT COUNT(*) AS ROWS_PRESENT FROM PEOPLE_T115;| ROWS_PRESENT |
|---|
| 12 |
The table is where it was. Nothing was added and nothing was taken away.
Can a table insert into itself?
It can. We are finished with the people table, so let's take its count one last time and then move over to the import table.
SELECT COUNT(*) AS ROWS_BEFORE FROM PEOPLE_T115;| ROWS_BEFORE |
|---|
| 12 |
Twelve rows, the same as the count in the section above. Now the import table, which holds the two rows we saw earlier. Here it selects from itself.
INSERT INTO IMPORT_T115 (SURNAME, GIVEN_NAME)
SELECT SURNAME, GIVEN_NAME FROM IMPORT_T115;| number of rows inserted |
|---|
| 2 |
Two rows inserted. Let's group the table by its own values and count the copies.
SELECT SURNAME, GIVEN_NAME, COUNT(*) AS COPIES
FROM IMPORT_T115 GROUP BY SURNAME, GIVEN_NAME ORDER BY SURNAME;| SURNAME | GIVEN_NAME | COPIES |
|---|---|---|
| Moreau | Marie | 2 |
| Nowak | Nina | 2 |
Every row is now there twice. The SELECT read the table as it stood when the statement began, and wrote all of it back in. There is no WHERE clause to stop that, so each run copies whatever it finds.
One last thing: putting the schema back
Both tables were made for this post, so both of them go.
DROP TABLE IF EXISTS PEOPLE_T115;| status |
|---|
| PEOPLE_T115 successfully dropped. |
DROP TABLE IF EXISTS IMPORT_T115;| status |
|---|
| IMPORT_T115 successfully dropped. |
SELECT CURRENT_DATABASE() AS THE_DATABASE,
CURRENT_SCHEMA() AS THE_SCHEMA,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
WHERE RIGHT(TABLE_NAME, 5) = '_T115') 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
- There are three spellings: one
VALUESrow, manyVALUESrows, andINSERT ... SELECT. - A column left out of the list takes its
DEFAULT, orNULLif it has not got one. - The keyword
DEFAULTworks inside aVALUESlist. - No column list means every column, in the order the table declares them.
- The number of values is checked: expecting 5 but got 3. A refused
INSERTleaves the table untouched. INSERTconverts silently. A string of digits goes into anINTand comes back a number; a number goes into aVARCHARand comes back text.- It refuses to convert when the value would not fit: a word in an
INTcolumn, or a string longer than the column allows. - The column list is matched by position, not by name. A correct-looking
INSERTcan put the surname inFIRST_NAMEwith no warning at all. - An
INSERTwhoseSELECTmatches nothing reports0and is not an error. - A table can insert into itself, and every row ends up with a second copy.
Episode 116 — INSERT OVERWRITE, INSERT ALL and INSERT FIRST. Three more shapes of the same statement. Between them they write seven rows from five, or three, or none at all, and one of them empties a table while reporting that it inserted nothing.
Every query, every result grid and every error on this page was captured from a live Snowflake account (version 10.35.101) running the demo script for this episode. The PEOPLE_T115 and IMPORT_T115 tables are created and dropped by the script, and the names in them are made up for teaching. Cells reading NULL are real nulls. Nothing on this page is timed and no performance claims are made anywhere in this series. INSERT behaviour follows the Snowflake INSERT documentation.



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