Introducing Chaos Genius for Databricks Cost Optimization

Join the waitlist

HOW TO: Use Snowflake INSERT INTO to Insert Data into Tables (2024)

Loading data into tables is one of the most common tasks you'll perform in Snowflake. To insert data into a table, you can use the Snowflake INSERT INTO command, which gives you the power to insert one or more rows of data into a table simply by specifying the target table, column values, and source of the data. It also allows you to update existing table data by adding additional rows. If needed, you can even replace or overwrite existing table rows entirely with new data.

In this article, we will cover everything you need to know about using the Snowflake INSERT INTO command, including syntax, practical examples, use cases, performance considerations—and so much more!!

How Do You Use the Insert Command in a Snowflake?

Before diving deep into the specifics of Snowflake INSERT INTO command, it's helpful to grasp the basic concept of Snowflake tables. Picture Snowflake tables as shelves within your data warehouse, each one neatly organizing your information into rows and columns. Each row in a table represents a distinct record, while each column denotes an attribute or property of that record. In Snowflake, tables are defined at the schema level, serving as crucial components for data storage and management. You can create a Snowflake table using the CREATE TABLE statement, wherein you specify a table name or identifier, column names, and their respective data types.

Once a table is created, you can start loading data into it using Snowflake INSERT INTO command. The core purpose of Snowflake INSERT INTO command is to populate table rows with data. It allows you to take data from various sources and efficiently insert it into Snowflake tables.

The syntax of Snowflake INSERT INTO is straightforward:

INSERT INTO table_name (column1, column2) 
VALUES (value1, value2);
Snowflake INSERT INTO syntax

As you can see, this will insert one row with the specified column values into the target table. You can insert multiple rows and perform more complex INSERT operations, as you will see in the examples below.

How do you INSERT data in Snowflake Table—Using Snowflake INSERT INTO?

Let's look at the full syntax and parameters supported for the Snowflake INSERT INTO command:

Here is the full syntax breakdown of Snowflake INSERT INTO command:

INSERT [ OVERWRITE ] INTO <target_table> [ ( <target_col_name> [ , ... ] ) ] { VALUES ( { <value> | DEFAULT | NULL } [ , ... ] ) [ , ( ... ) ]  | <query> }
Snowflake INSERT INTO full syntax

The key parameters of Snowflake INSERT INTO command are:

  • <target_table>: It is the name of the table where you want to insert the data. You can optionally specify the DB and Schema name as well, such as db_name.schema_name.table_name
  • <target_col>: It is the list of columns in the target table that you want to insert the data into. You can omit this parameter if you want to insert data into all the columns of the table, or if the order and number of columns in the source match the order and number of columns in the target table.
  • VALUES ( <value_list> ): It specifies one or more values to insert into the corresponding columns in the target table. In a VALUES clause, you can specify literal values, expressions, DEFAULT to use column defaults, or NULL for null values. Each value must be separated by a comma. You can insert multiple rows by specifying additional comma-separated value sets in the clause.
  • <query>: It specifies a full SQL query statement that returns values to be inserted into the corresponding columns of the target table. This allows you to efficiently insert rows selected from one or more source tables.
  • OVERWRITE: This optional clause specifies that the target table should be truncated before inserting the new values. It replaces all existing table data entirely with the rows being inserted. OVERWRITE option provides a convenient way to reset a table without additional delete logic. Specifying OVERWRITE does not affect access control privileges on the table. Additionally, INSERT OVERWRITE statements can process within the current transaction scope avoiding an explicit commit like a TRUNCATE would.

To provide a basic idea of how to use Snowflake INSERT INTO command in Snowflake, let's consider a simple example. Suppose we have a table called students with the following columns and data:

Snowflake table named “Students” - Snowflake INSERT INTO
Snowflake table named “Students” - Snowflake INSERT INTO

Now, we want to insert a new row of data into this table. To do so, we can use Snowflakle INSERT INTO command as follows:

INSERT INTO students (student_id, first_name, last_name, grade, major)
VALUES (4, 'Larry', 'Page', 10, 'Economics');
Snowflake INSERT INTO example
Inserting data into table using Snowflake INSERT INTO command
Inserting data into table using Snowflake INSERT INTO command

This will insert a new row in the students table with the specified student_id, first_name, last_name, grade, and major VALUES. As you can see, Snowflake INSERT INTO command is a simple and effective way to load data into a Snowflake table.

How Can I Check if Data Is Being Inserted Into a Table?

After you use Snowflake INSERT INTO command to load data into a Snowflake table, you might want to verify that the data is being inserted correctly and accurately. There are a few ways to do this, depending on your needs and preferences.

1) Selecting from the Table

The simplest and most common way to check if data is being inserted into a table is to use the SELECT statement to query the table and view the data. SELECT statement allows you to retrieve data from one or more tables, based on various criteria and options. You can use the SELECT statement to view the entire table, or a subset of the table, such as specific columns, rows, or values.

For instance, to review the data inserted in the previous example, we can easily use a SELECT statement by selecting all to view all the data in the table and verify whether the data was inserted or not. To do this, simply execute the query statement mentioned below:

SELECT * FROM students;
Selecting all from students table - Snowflake INSERT INTO
Selecting all from students table - Snowflake INSERT INTO

As you can see, this will return all the data from the students table, and we can see that the new row of data is inserted correctly

We can also use the SELECT statement to view only the newly inserted data, by using a condition or a filter. For example, we can use the WHERE clause to specify that we only want to see the data where the student major is “Economics”:

SELECT * FROM students WHERE major = 'Economics';
Specifying WHERE clause to view only the newly inserted data - Snowflake INSERT INTO
Specifying WHERE clause to view only the newly inserted data - Snowflake INSERT INTO

SELECT statement is a powerful and flexible way to check if data is being inserted into a table, as it allows you to view the data in various ways and formats. You can use different clauses, functions, and operators to customize your query and get the data you need.

OR

2) Using QUERY_HISTORY and ACCOUNT_USAGE views

Another way to check if data is being inserted into a table is to use the Query History view or the Account Usage to track the queries that were executed on the table. These features allow you to see who inserted data into the table, when they did it, how long it took, and other details.

You can also see details on all INSERT operations using the Snowflake QUERY_HISTORY and ACCOUNT_USAGE views.

For example:

SELECT * FROM snowflake.account_usage.query_history WHERE query_text LIKE 'INSERT INTO%' ORDER BY start_time DESC;
Snowflake INSERT INTO example
Using QUERY_HISTORY and ACCOUNT_USAGE views  to check if data was inserted- Snowflake INSERT INTO
Using QUERY_HISTORY and ACCOUNT_USAGE views to check if data was inserted- Snowflake INSERT INTO

As you can see, this will show all the Snowflake INSERT INTO statements, when they ran, and various other details. The ACCOUNT_USAGE view provides metrics on all DML operations like INSERT that can be useful for monitoring and optimization.

As you can see, the output will show you the details of the Snowflake INSERT INTO statements, when they ran, and various other details, such as the query ID, the user who issued the query, the role that was active, the warehouse that was used, the start and end time, execution status–and so much more!

OR

3) Using Snowsight

There is another alternative method—using Snowsight. So to access the entire detailed info of the query, head over to your Activity menu and click on Query History. Select the Filter dropdown option and enable SQL. Then, type the query text you want to search. For instance, if you're searching for the Snowflake INSERT INTO students query, simply type that and apply the filter. You should then be able to see the complete details, including the status of the query and the user who executed it.

Using Snowsight to check if data was inserted- Snowflake INSERT INTO
Using Snowsight to check if data was inserted- Snowflake INSERT INTO

Practical Examples of Using Snowflake INSERT INTO Command

Now let’s look at some practical examples of how to use Snowflake INSERT INTO command in Snowflake with different scenarios and options. We will use a demo database called demo_customers_db and a demo schema called demo_customers_schema for these examples. You can create these objects by using the following commands:

CREATE DATABASE demo_customers_db;
USE DATABASE demo_customers_db;
CREATE SCHEMA demo_customers_schema;
USE SCHEMA demo_customers_schema;
Snowflake INSERT INTO practical example
Creating Snowflake Database and Schema - Snowflake INSERT INTO
Creating Snowflake Database and Schema - Snowflake INSERT INTO

We will also create and use some sample tables for these examples. You can create these tables by using the following commands:

-- Creating customers table with four columns: id, name, email, and phone
CREATE TABLE customers (
  id INT,
  name VARCHAR,
  email VARCHAR,
  phone VARCHAR
);

-- Creating orders table with four columns: id, customer_id, product, and amount
CREATE TABLE orders (
  id INT,
  customer_id INT,
  product VARCHAR,
  amount DECIMAL(10,2)
);

-- Creating products table with three columns: id, name, and price
CREATE TABLE products (
  id INT,
  name VARCHAR,
  price DECIMAL(10,2)
);

-- Creating json_data with two columns: id and data
CREATE TABLE json_data (
  id INT,
  data VARIANT
);

Now, let’s see some examples of how to use Snowflake INSERT INTO command in Snowflake.

Example 1 — Inserting Into Single Row

The simplest way to use Snowflake INSERT INTO command is to insert a single row of data into a table. You can do this by using the VALUES clause and specifying the values for each column. For example, suppose we want to insert one row of data into the customers table, you can execute the following command:

INSERT INTO customers (id, name, email, phone) VALUES (1, 'Chaos', '[email protected]', '123-456');
Snowflake INSERT INTO Single Row practical example

As you can see, this will insert the new row of data into the customers table.

Inserting Into Single Row using Snowflake INSERT INTO
Inserting Into Single Row using Snowflake INSERT INTO

Example 2 — Inserting Into Multiple Rows

Now, to insert multiple rows of data into a table, you can utilize the VALUES clause and specify multiple value lists separated by commas. For instance, if you wish to insert two new rows of data into the customers table, you can execute the following command:

INSERT INTO customers (id, name, email, phone) VALUES
(2, 'Genius', '[email protected]', '789-1011'),
(3, 'Elon', '[email protected]', '121-3141');
Snowflake INSERT INTO Multiple Row practical example

As you can see, this will insert the two new rows of data into the customers table.

Inserting Multi-Row using Snowflake INSERT INTO
Inserting Multi-Row using Snowflake INSERT INTO
INSERT INTO orders VALUES
  (1, 1, 'Laptop', 999.99),
  (2, 1, 'Camaera', 199.99);
Snowflake INSERT INTO Multiple Row practical example

As you can see, this will insert the two new rows of data into the orders table.

Inserting Multi-Row using Snowflake INSERT INTO
Inserting Multi-Row using Snowflake INSERT INTO
INSERT INTO products VALUES
  (1, 'Laptop', 999.99),
  (2, 'Camaera', 199.99);
Snowflake INSERT INTO Multiple Row practical example

As you can see, this will insert the two new rows of data into the products table.

Inserting Multi-Row using Snowflake INSERT INTO
Inserting Multi-Row using Snowflake INSERT INTO

Example 3 — Using Subqueries and CTEs for Complex Data Insertion

Another way to use Snowflake INSERT INTO command is to insert data from a subquery or a common table expression (CTE) into a table. A subquery is a query that is nested inside another query, and a CTE is a temporary named result set that can be referenced within another query. You can use subqueries and CTEs to insert data from complex or derived sources, such as joins, aggregations, or transformations. For instance, suppose we want to insert data into the customers table using a subquery. We can use Snowflake INSERT INTO command as follows:

Using Subquery:

INSERT INTO customers (id, name, email, phone)
SELECT 4, 'Jeff', '[email protected]', '7788-990'
WHERE NOT EXISTS (SELECT 1 FROM customers WHERE name='Jeff');
Snowflake INSERT INTO using Subqueries practical example

As you can see, this will insert the data using Subqueries into the customers table.

Inserting data using Subqueries - Snowflake INSERT INTO
Inserting data using Subqueries - Snowflake INSERT INTO

Using CTEs:

INSERT INTO orders (customer_id, product, amount)
WITH new_cust_order AS (
  SELECT 1 AS customer_id, p.name, p.price 
  FROM products p
  WHERE p.name = 'Laptop'  
)
SELECT * FROM new_cust_order;
Snowflake INSERT INTO using Subqueries practical example

As you can see, this will insert the data from the CTE into the orders table.

Inserting data using CTes- Snowflake INSERT INTO
Inserting data using CTes- Snowflake INSERT INTO

Using subqueries and CTEs for data insertion is a powerful and flexible way to load data into a Snowflake table, as it allows you to insert data from complex or derived sources.

Example 4 — Inserting Multiple Rows for JSON Data

Another way to use Snowflake INSERT INTO command is to insert multiple rows of data for JSON data into a table. JSON is a popular data format that can store nested and hierarchical data, such as arrays and objects. Snowflake supports JSON data as a native data type, called VARIANT, which can store any JSON value. You can use Snowflake INSERT INTO command to insert JSON data into a VARIANT column, by using the PARSE_JSON (function to convert the JSON string into a VARIANT value).

For example, suppose we want to insert one new row of data into the json_data table, which we created earlier, with the following JSON values:

{
  "first_name": "Elon",
  "last_name": "Musk",
  "Net worth": "250B",
}

Now, we can use Snowflake INSERT INTO command as follows:

INSERT INTO json_data(id, data) select 1, parse_json($${
  "first_name": "Elon",
  "last_name": "Musk",
  "Net worth": "250B",
}$$);
Snowflake INSERT INTO using JSON data practical example
Inserting json data into the json_data table - Snowflake INSERT INTO
Inserting json data into the json_data table - Snowflake INSERT INTO

As you can see, this  will insert the one new rows of data into the json_data table, and the table will look like this:

Inserting json data into the json_data table - Snowflake INSERT INTO
Inserting json data into the json_data table - Snowflake INSERT INTO

Example 5 — Inserting Using Overwrite

Now, let's insert data into a table using the OVERWRITE option. The OVERWRITE option allows you to delete the existing data in the table and replace it with the new data. This can be useful when you want to refresh the table with the latest data, or when you want to avoid duplicates or conflicts. To use the OVERWRITE option, you need to specify OVERWRITE as an optional parameter in Snowflake INSERT INTO command.

For example,

Here is our initial table data:

Initial Snowflake table data - Snowflake INSERT INTO
Initial Snowflake table data

Let’s suppose you want to overwrite the existing data with new data, you can execute the following query:

INSERT OVERWRITE INTO customers VALUES (1, 'Chaos', '[email protected]', '123-456');  
Snowflake INSERT INTO overwriting data practical example
Using OVERRITE to overwrite the existing data with new data
Using OVERRITE to overwrite the existing data with new data

Example 6 — Inserting Into Multiple Columns

Now, let's use the Snowflake INSERT INTO command to insert data into multiple columns of a table. You can achieve this by specifying the column names and their corresponding values within the INSERT INTO command. For instance, if you intend to insert data into the products table while specifically targeting the name and price columns, leaving the id column empty or null, you can execute the following Snowflake INSERT INTO command:

INSERT INTO products (name, price) VALUES
('Mouse', 69.00),
('Keyboard', 420.00),
('Monitor', 500.00);
Snowflake INSERT INTO Multiple columns practical example



Inserting Into Multiple Columns

As you can see, this will insert the data into the name and price columns of the products table, and the id column will be null, and the table will look like this:

Inserting Into Multiple Columns - Snowflake INSERT INTO

Example 7 — Inserting Into Temp Table

Snowflake temporary table is a table that exists only for the duration of the session, and is automatically dropped when the session ends. You can use temporary tables to store intermediate or temporary results or to perform complex or iterative operations. To create a temporary table, you need to use the TEMPORARY keyword in the CREATE TABLE statement. To insert data into a temporary table, you can use the INSERT INTO command as usual.

For example, suppose you want to create a temporary table called temp_orders that stores the orders that have an amount greater than 10, and then insert data into this table from the orders table. We can use the INSERT INTO command as follows, but first let’s create a temporary table, to do so:

CREATE TEMPORARY TABLE temp_orders (
  id INT,
  customer_id INT,
  product VARCHAR,
  amount DECIMAL(10,2)
);
Snowflake INSERT INTO Temp table practical example

Now, let’s insert it.

INSERT INTO temp_orders (id, customer_id, product, amount)
SELECT id, customer_id, product, amount
FROM orders
WHERE amount > 10;
Snowflake INSERT INTOTemp table practical example

As you can see, this will create a temporary table called temp_orders and insert the data from the orders table where the amount is greater than 10 into the temp_orders table, and the table will look like this:

Inserting Into Temp Table - Snowflake INSERT INTO
Inserting Into Temp Table - Snowflake INSERT INTO

Use Cases of Snowflake INSERT INTO command

Snowflake INSERT INTO command is incredibly versatile—it can serve a wide variety of use cases for loading data into Snowflake. Let's explore some of the most common ways INSERT INTO gets used.

1) Simple Data Population

One of the simplest but most common uses of Snowflake INSERT INTO is to initially populate tables with data from explicitly specified values or literals.

2) Continual Data Updates

Another use case of Snowflake INSERT INTO command is to update the existing data in a table with new or modified data. This can be extremely useful when you want to refresh or modify the data in a table, or when you want to handle duplicates or conflicts in the data. You can use the INSERT INTO command with the OVERWRITE option to delete the existing data in the table and replace it with the new data.

3) Incremental Data Loads

Another use case of Snowflake INSERT INTO command is to perform incremental data loads into a table. Incremental data loads are a type of data loading technique that involves loading only the new or changed data into a table, rather than loading the entire data set. This can be useful when you want to optimize the performance and efficiency of your data-loading process, or when you want to avoid loading duplicate or redundant data into a table. You can use Snowflake INSERT INTO command with a query that filters out the existing data in the table and only selects the new or changed data from the source.

Some Examples:

  • Daily inserts of new customer signups exported from a CRM system
  • Hourly inserts of the latest sales orders
  • Daily inserts of any profile updates from a social media site's API

Performance Considerations of Snowflake INSERT INTO command

Whenever you are inserting data into the Snowflake table, you'll want to optimize the process to maximize throughput and minimize load times.

Here are some key best practices to maximize the efficiency when inserting data into Snowflake:

1) Use Multi-Row Inserts

When possible, use a single INSERT statement with multiple VALUE sets to insert multiple rows at once. This is much faster than separate single-row INSERTs as it reduces the number of network round trips and cuts down on transaction overhead. Multi-row inserts allow inserting thousands of rows in one shot very efficiently.

2) Use Snowpipe

For loading higher volumes of data (over lets say 500K rows), utilize Snowpipe to auto-ingest from cloud storage. Snowpipe handles inserting new files as they arrive in cloud platforms like. This hands-off approach removes the need for manual INSERTs and provides continuous, scalable loading.

3) Limit the Number of Columns

Avoid inserting excess columns you don't need. INSERT only the required columns to reduce inserted data volume and increase speed.

Conclusion

And that’s a wrap! Snowflake INSERT INTO command is the foundation for efficiently loading data in Snowflake tables. It lets you add one or more rows of information into a table simply by specifying the target table, column values, and source of the data. Plus, it's not just about adding new stuff; you can also update what's already in the table by adding more rows. And if necessary, you can completely swap out the old table rows with all-new data.

In this article, we covered:

  • How do you use the insert command in a snowflake?
  • How do you insert data in snowflake table—using snowflake insert into?
  • How can I check if data is being inserted into a table?
  • Practical examples of using snowflake insert into command
  • Use cases of snowflake insert into command
  • Performance considerations of snowflake insert into command

FAQs

What is the basic syntax of Snowflake INSERT INTO?

The basic syntax is:

INSERT INTO table_name (column1, column2) VALUES (value1, value2);

How do you insert multiple rows in Snowflake?

Use multiple VALUE sets separated by commas:

INSERT INTO table_name VALUES
(val1, val2),
(val3, val4);

Can you insert data into specific columns in Snowflake?

Yes, specify the column names in the INSERT statement:

INSERT INTO table_name (col1, col3)
VALUES (val1, val3);

How do you insert data from another table in Snowflake?

You can use SELECT query:

INSERT INTO table2
SELECT * FROM table1;

How can you insert JSON data into Snowflake?

Use PARSE_JSON to convert to VARIANT:

INSERT INTO json_table (doc)
VALUES (PARSE_JSON('{key:value}'));

What is the OVERWRITE option in Snowflake INSERT INTO?

OVERWRITE replaces the existing table data with the newly inserted rows.

Can you update data by inserting in Snowflake?

No native upsert, but you can MERGE or use INSERT and UPDATE.

What happens when inserting a row that violates constraints?

The statement fails and no rows are inserted.

Is it possible to insert Semi-structured data in Snowflake?

Yes. Use a VARIANT column to insert JSON, XML, arrays, etc.

Can you insert data directly from files into Snowflake?

Yes, use COPY INTO <table> or the Snowpipe auto-ingest feature.

How to check if an insert worked in Snowflake?

Query the table data to check rows inserted or use query history.

Is there a row limit for Snowflake INSERT INTO?

No set limit, you can insert millions or billions of rows.

Can you insert into multiple tables with one Snowflake INSERT INTO statement?

No, each INSERT statement can only be inserted into one table.

What permissions do you need to INSERT data in Snowflake?

The INSERT privilege on the table, or ownership of the table.

Is the INSERT INTO syntax different for temporary tables in Snowflake?

No, the INSERT INTO syntax is identical for temporary and permanent tables.

Can you insert data into multiple tables with one Snowflake INSERT INTO statement?

No, each INSERT statement can only insert into a single table. You need separate INSERT statements for each target table.

Pramit Marattha

Technical Content Lead

Pramit is a Technical Content Lead at Chaos Genius.

People who are also involved

“Chaos Genius has been a game-changer for our DataOps at NetApp. Thanks to the precise recommendations, intuitive interface and predictive capabilities, we were able to lower our Snowflake costs by 28%, yielding us a 20X ROI

Chaos Genius has given us a much better understanding of what's driving up our data-cloud bill. It's user-friendly, pays for itself quickly, and monitors costs daily while instantly alerting us to any usage anomalies.

Anju Mohan

Director, IT

Simon Esprit

Chief Technology Officer

Join today to get upto
30% Snowflake
savings

Join today to get upto 30% Snowflake savings

Unlock Snowflake Savings Join waitlist
Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.