Introducing Chaos Genius for Databricks Cost Optimization

Get started

HOW TO: Modify Table Records With Snowflake UPDATE (2024)

In Snowflake, the UPDATE command is powerful for modifying existing data within tables. It allows you to change the values of specific columns in one or more rows based on conditions you define. In this article, we'll dive deep into the Snowflake UPDATE statement, covering its syntax, use cases, and best practices for optimal performance and data integrity.

What Is Snowflake UPDATE Statement?

Snowflake UPDATE statement is used to modify existing records within a table. It can update one or more columns for the specified rows.

The power of the UPDATE statement lies in its flexibility. You can update a single row, multiple rows, or even all rows in a table. You can modify one column or several columns simultaneously.

Syntax and Basic Usage of Snowflake UPDATE

To effectively use the Snowflake UPDATE statement, it's crucial to understand its syntax and parameters. Let's break it down:

Basic Syntax:

UPDATE <target_table>
   SET <column1> = <value1>, <column2> = <value2>, ...
   [ FROM <additional_tables> ]
   [ WHERE <condition> ]
Snowflake UPDATE statement syntax

Parameters:

  • target_table: Specifies the table you want to update.
  • column1, column2, ...: Columns in the target_table you wish to update.
  • value1, value2, ...: New values to assign to the specified columns.
  • FROM additional_tables (optional): Additional tables used for selecting rows to update or setting new values.
  • WHERE condition (optional): Condition to specify which rows should be updated. If you omit this clause, all rows in the table will be updated.

Basic Snowflake Update Example:

Let's look at a simple example to illustrate the basic usage:

UPDATE customers
SET email = '[email protected]'
WHERE customer_id = 1001;
Basic Snowflake Update Example

Save up to 30% on your Snowflake spend in a few minutes!

Enter your work email
Enter your work email
Snowflake Background Snowflake Background

Practical Examples of Basic Snowflake UPDATE Operations

To truly grasp the power and flexibility of Snowflake UPDATE statement, let's explore some practical examples that cover various scenarios you might encounter in real-world data management tasks.

Assume we have a table called students with the following structure:

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    first_name VARCHAR,
    last_name VARCHAR,
    major VARCHAR,
    gpa NUMBER(3,2)
);
Creating Students table - Snowflake UPDATE statement

Now, lets populate it with some sample data:

INSERT INTO students (student_id, first_name, last_name, major, gpa)
VALUES 
    (101, 'David', 'Attenborough', 'Biology', 3.8),
    (102, 'Mark', 'Zuck', 'Computer Science', 3.5),
    (103, 'Wilhelm', 'Wundt', 'Psychology', 3.2),
    (104, 'Elon', 'Musk', 'Engineering', 3.9);
Populating some sample data in Students table - Snowflake UPDATE statement
Creating Students table and populating some sample data - Snowflake UPDATE statement

Example 1—Update a Single Row using Snowflake UPDATE Statement

Suppose you need to update the major for a specific student in your database:

UPDATE students
SET major = 'Neuroscience'
WHERE student_id = 103;
Updating Single Row using Snowflake UPDATE Statement
Updating Single Row using Snowflake UPDATE Statement
Updating Single Row using Snowflake UPDATE Statement

This updates Wilhelm's major from 'Psychology' to 'Neuroscience'.

Example 2—Update Multiple Rows using Snowflake UPDATE Statement

In some cases, you might need to update multiple rows that meet certain criteria. For instance, let's say you want to increase GPA by 0.1 to all students majoring in Biology or Engineering.

UPDATE students
SET gpa = gpa + 0.1
WHERE major = 'Biology' OR major = 'Engineering';
Updating Multiple Rows using Snowflake UPDATE Statement
Updating Multiple Rows using Snowflake UPDATE Statement
Updating Multiple Rows using Snowflake UPDATE Statement

Example 3—Update All Rows using Snowflake UPDATE Statement

While less common, there are scenarios where you might need to update all rows in a table. For example, if you want to give everyone a perfect GPA:

UPDATE students
SET gpa = 4.0; 
Updating All Rows using Snowflake UPDATE Statement
Updating All Rows using Snowflake UPDATE Statement
Updating All Rows using Snowflake UPDATE Statement
CAUTION: This is a blanket update, setting all students' GPAs to 4.0. Be extremely careful with updates without a WHERE clause!

Example 4—Update Multiple Columns using Snowflake UPDATE Statement

Snowflake allows you to update multiple columns in a single statement, which can be more efficient than running multiple separate updates:

UPDATE students
SET last_name = 'Bezos', major = 'Data Science'
WHERE student_id = 102;
Updating Multiple Columns using Snowflake UPDATE Statement
Updating Multiple Columns using Snowflake UPDATE Statement
Updating Multiple Columns using Snowflake UPDATE Statement

As you can see, Mark's last name is changed to Bezos, and his major is switched to Data Science.

Example 5—Using Snowflake UPDATE Command with JOINS

Sometimes, you need to update data in one table based on information from another table. Snowflake UPDATE statement supports this through the use of Snowflake Joins:

First, let's create a grade_changes table:

CREATE TABLE grade_changes (
    student_id INT,
    gpa_change NUMBER(3,2)
);

INSERT INTO grade_changes (student_id, gpa_change)
VALUES (101, -0.2), (104, -0.3);
Creating grade_changes table and populating some sample data - Snowflake UPDATE statement

Now the update:

UPDATE students s
SET gpa = gpa + gc.gpa_change
FROM grade_changes gc
WHERE s.student_id = gc.student_id;
Updating Snowflake UPDATE Command with JOINS
Updating Snowflake UPDATE Command with JOINS
Updating Snowflake UPDATE Command with JOINS

As you can see, This joins the two tables on student_id and applies GPA changes based on the grade_changes table. David's GPA decreases by 0.2, and Elon's GPA decreases by 0.3.

Best Practices for Using Snowflake UPDATE Statement

Snowflake UPDATE statement is powerful and flexible but use it wisely to preserve data integrity and performance. Here are some best practices to follow:

1) Always use a WHERE clause

Unless you intentionally want to update every row in a table, always include a WHERE clause in your Snowflake UPDATE statement. This will prevent accidental mass updates and ensure you’re only updating the data you intend to.

2) Test updates in a staging environment

Before running Snowflake UPDATE statements on your production data, test them in a staging environment. This will allow you to verify the results and ensure the update works as expected without touching your live data. You can use Snowflake cloning feature to create a copy of your production table for testing.

3) Use Streams and Tasks for complex transformations

For more complex or recurring updates, use Snowflake Streams and Snowflake Tasks. Streams can capture changes to your source data, Tasks can automate the process of applying those changes to your target tables. This is useful for maintaining slowly changing dimensions or for incremental updates in a data warehouse.

4) Consider the Use of Temporary Tables for Large-Scale Updates

When doing large updates with complex logic or multiple data sources, using Snowflake temporary tables will improve performance and reduce the chance of errors.

5) Monitor and Adjust Resource Usage Using Resource Monitors

Large UPDATEs can consume a lot of resources. To prevent runaway queries and control costs use Snowflake Resource Monitors to set limits on warehouse usage.

6) Update in Batches (For Large Datasets)

If you need to update a huge number of rows, consider breaking the operation into smaller batches to avoid locking the table for too long and impacting other users.

7) Analyze Impact with EXPLAIN

Use the Snowflake EXPLAIN command before running your Snowflake UPDATE statement to see how Snowflake will execute it. This will help you spot bottlenecks or inefficiencies.

8) Add Comments

Add comments to explain the purpose of your Snowflake UPDATE statements, especially for more intricate update logic.

Want to take Chaos Genius for a spin?

It takes less than 5 minutes.

Enter your work email
Enter your work email
Snowflake Logo

Further Reading

For more detailed information on using the Snowflake UPDATE statement, refer to the official Snowflake Documentation.

Conclusion

And that's a wrap! Snowflake UPDATE statement is a super powerful and flexible way to modify data in Snowflake. It can handle anything from single row updates to multi-table updates. To get accurate data, you need to master this statement. We've gone over the basics, examples, and best tips for performance and data integrity. Just remember, with great power comes great responsibility—use it wisely! Now, keep exploring, learning, and, most importantly, maintaining accurate data with Snowflake UPDATE!

FAQs

What happens if I omit the WHERE clause in a Snowflake UPDATE statement?

Leaving out the WHERE clause updates all rows in the table which can be big changes and performance issues. Always use a WHERE clause to limit your updates.

Can I update multiple columns in a single Snowflake UPDATE statement?

Yes, you can update multiple columns in a single statement by separating each column-value pair with a comma.

What are the benefits of using Streams and Tasks for data updates in Snowflake?

Streams and Tasks allow you to capture data changes and automate complex transformations, making updates more efficient and manageable.

How can I recover data if an update operation goes wrong?

Snowflake Time Travel and Fail-safe features let you query, clone and restore historical data, so you have data recovery mechanisms.

Can you perform a Snowflake UPDATE using a CASE statement?

Yes, you can use a CASE statement within an UPDATE to conditionally update values.

What happens if an UPDATE statement results in a non-deterministic join?

Snowflake can handle non-deterministic updates (where multiple rows from the source match a single row in the target) by setting the ERROR_ON_NONDETERMINISTIC_UPDATE session parameter. If set to TRUE, it raises an error; if FALSE (default), it performs the update but the result is non-deterministic.

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.