Snowflake SQL Tutorial 115 - INSERT Matches By Position, Not By Name

Snowflake SQL · Episode 115

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

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 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
-- 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.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, LAST_NAME, CITY, JOINED)
VALUES (1, 'Ana', 'Alvarez', 'Lisbon', '2026-01-10'::DATE);
Result1 row · 1 column
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.

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

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, LAST_NAME, CITY, JOINED)
SELECT 5, 'Eve', 'Eriksen', 'Bergen', '2026-03-01'::DATE;
Result1 row · 1 column
number of rows inserted
1

Let's look at the table.

Query
SELECT PERSON_ID, FIRST_NAME, LAST_NAME, CITY, JOINED
FROM PEOPLE_T115 ORDER BY PERSON_ID;
Result5 rows · 5 columns
PERSON_IDFIRST_NAMELAST_NAMECITYJOINED
1AnaAlvarezLisbon2026-01-10
2BenBrandtBerlin2026-01-22
3CleoCostaPorto2026-02-03
4DanDahlOslo2026-02-15
5EveEriksenBergen2026-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.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES (6, 'Finn');
Result1 row · 1 column
number of rows inserted
1

One row inserted, same as before. Let's go and look at it.

Query
SELECT PERSON_ID, FIRST_NAME, LAST_NAME, CITY, JOINED
FROM PEOPLE_T115 WHERE PERSON_ID = 6;
Result1 row · 5 columns
PERSON_IDFIRST_NAMELAST_NAMECITYJOINED
6FinnNULLunknownNULL

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.

Query
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;
Result1 row · 4 columns
ROWS_TOTALROWS_WITH_A_LAST_NAMEROWS_WITH_A_CITYROWS_THAT_TOOK_THE_DEFAULT
6561

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.

Three columns, not two

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.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, CITY)
VALUES (7, 'Gus', DEFAULT);
Result1 row · 1 column
number of rows inserted
1
Query
SELECT PERSON_ID, FIRST_NAME, CITY FROM PEOPLE_T115 WHERE PERSON_ID = 7;
Result1 row · 3 columns
PERSON_IDFIRST_NAMECITY
7Gusunknown

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.

Query
INSERT INTO PEOPLE_T115
VALUES (8, 'Hana', 'Haas', 'Vienna', '2026-03-20'::DATE);
Result1 row · 1 column
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.

Query
INSERT INTO PEOPLE_T115
VALUES (9, 'Iris', 'Ibsen');
Query failed — Snowflake returned an error
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.

Query
INSERT INTO PEOPLE_T115
VALUES (10, 'Jon', 'Jansen', 'Malmo', '2026-04-01'::DATE, 'extra');
Query failed — Snowflake returned an error
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.

Query
SELECT COUNT(*) AS ROWS_PRESENT FROM PEOPLE_T115;
Result1 row · 1 column
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.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES ('11', 'Kai');
Result1 row · 1 column
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.

Query
SELECT PERSON_ID, FIRST_NAME, PERSON_ID + 1 AS IT_IS_A_NUMBER_IF_THIS_WORKS
FROM PEOPLE_T115 WHERE FIRST_NAME = 'Kai';
Result1 row · 3 columns
PERSON_IDFIRST_NAMEIT_IS_A_NUMBER_IF_THIS_WORKS
11Kai12

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.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES (12, 99999);
Result1 row · 1 column
number of rows inserted
1
Query
SELECT PERSON_ID, FIRST_NAME, LENGTH(FIRST_NAME) AS LENGTH_AS_TEXT
FROM PEOPLE_T115 WHERE PERSON_ID = 12;
Result1 row · 3 columns
PERSON_IDFIRST_NAMELENGTH_AS_TEXT
12999995

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.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES ('not a number', 'Lena');
Query failed — Snowflake returned an error
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.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
VALUES (13, 'a first name far longer than thirty characters');
Query failed — Snowflake returned an error
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.

Query
CREATE OR REPLACE TABLE IMPORT_T115 (
    SURNAME    VARCHAR(30),
    GIVEN_NAME VARCHAR(30)
);
Result1 row · 1 column
status
Table IMPORT_T115 successfully created.
Query
INSERT INTO IMPORT_T115 (SURNAME, GIVEN_NAME)
VALUES ('Moreau', 'Marie'),
       ('Nowak',  'Nina');
Result1 row · 1 column
number of rows inserted
2
Query
SELECT SURNAME, GIVEN_NAME FROM IMPORT_T115 ORDER BY SURNAME;
Result2 rows · 2 columns
SURNAMEGIVEN_NAME
MoreauMarie
NowakNina

Two rows. Moreau, Marie and Nowak, Nina.

Now the insert, and I want you to read this one before you look at the result.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, LAST_NAME)
SELECT 20, SURNAME, GIVEN_NAME FROM IMPORT_T115 WHERE SURNAME = 'Moreau';
Result1 row · 1 column
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.

Query
SELECT PERSON_ID, FIRST_NAME, LAST_NAME
FROM PEOPLE_T115 WHERE PERSON_ID = 20;
Result1 row · 3 columns
PERSON_IDFIRST_NAMELAST_NAME
20MoreauMarie

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.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME, LAST_NAME)
SELECT 21, GIVEN_NAME, SURNAME FROM IMPORT_T115 WHERE SURNAME = 'Nowak';
Result1 row · 1 column
number of rows inserted
1
Query
SELECT PERSON_ID, FIRST_NAME, LAST_NAME
FROM PEOPLE_T115 WHERE PERSON_ID IN (20, 21) ORDER BY PERSON_ID;
Result2 rows · 3 columns
PERSON_IDFIRST_NAMELAST_NAME
20MoreauMarie
21NinaNowak

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.

Query
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);
Result1 row · 3 columns
SURNAMES_IN_THE_FIRST_NAME_COLUMNGIVEN_NAMES_IN_THE_FIRST_NAME_COLUMNROWS_FROM_THE_IMPORT
112
The thing to take away

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.

Query
INSERT INTO PEOPLE_T115 (PERSON_ID, FIRST_NAME)
SELECT 99, 'Nobody' WHERE 1 = 0;
Result1 row · 1 column
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.

Query
SELECT COUNT(*) AS ROWS_PRESENT FROM PEOPLE_T115;
Result1 row · 1 column
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.

Query
SELECT COUNT(*) AS ROWS_BEFORE FROM PEOPLE_T115;
Result1 row · 1 column
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.

Query
INSERT INTO IMPORT_T115 (SURNAME, GIVEN_NAME)
SELECT SURNAME, GIVEN_NAME FROM IMPORT_T115;
Result1 row · 1 column
number of rows inserted
2

Two rows inserted. Let's group the table by its own values and count the copies.

Query
SELECT SURNAME, GIVEN_NAME, COUNT(*) AS COPIES
FROM IMPORT_T115 GROUP BY SURNAME, GIVEN_NAME ORDER BY SURNAME;
Result2 rows · 3 columns
SURNAMEGIVEN_NAMECOPIES
MoreauMarie2
NowakNina2

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.

Query
DROP TABLE IF EXISTS PEOPLE_T115;
Result1 row · 1 column
status
PEOPLE_T115 successfully dropped.
Query
DROP TABLE IF EXISTS IMPORT_T115;
Result1 row · 1 column
status
IMPORT_T115 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) = '_T115') 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

  • There are three spellings: one VALUES row, many VALUES rows, and INSERT ... SELECT.
  • A column left out of the list takes its DEFAULT, or NULL if it has not got one.
  • The keyword DEFAULT works inside a VALUES list.
  • 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 INSERT leaves the table untouched.
  • INSERT converts silently. A string of digits goes into an INT and comes back a number; a number goes into a VARCHAR and comes back text.
  • It refuses to convert when the value would not fit: a word in an INT column, or a string longer than the column allows.
  • The column list is matched by position, not by name. A correct-looking INSERT can put the surname in FIRST_NAME with no warning at all.
  • An INSERT whose SELECT matches nothing reports 0 and is not an error.
  • A table can insert into itself, and every row ends up with a second copy.
Next in this series

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.