Database Management
- How to Add a Default Value to a Column
- How to Add a Column
- How to Add a NOT NULL Constraint
- How to Alter Sequence
- How to Create a Table
- How to Create a View
- How to Create an Index
- How to Drop a Column
- How to Drop a Table
- How to Drop a View
- How to Drop an Index
- How to Duplicate a Table
- How to Remove a Default Value to a Column
- How to Remove a NOT NULL Constraint
- How to Rename a Column
- How to Rename a Table
- How to Truncate a Table
Dates and Times
Analysis
- How to Do Type Casting
- How to Avoid Gaps in Data
- How to Calculate Cumulative Sum/Running Total
- How to Calculate Percentiles
- How to Compare Two Values When One is NULL
- How to Get First Row Per Group
- How to Have Multiple Counts
- How to Upload CSV
- How to Query a JSON Object
- How to Use Coalesce
- How to Write a Case Statement
- How to Write a Common Table Expression
How to Update Data in Snowflake
Snowflake is a powerful cloud data platform that makes managing data easy. One key operation is updating existing data to reflect new information or correct errors. In this tutorial, we’ll walk you through the steps to update data in Snowflake using SQL.
1. Understand the UPDATE
Statement
The UPDATE
statement in Snowflake lets you modify existing records in a table. The basic syntax looks like this:
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
The WHERE
clause is critical — it ensures only the intended rows are updated. Without it, all rows in the table will be updated.
2. Example: Updating a Single Record
UPDATE customers
SET status = 'Active'
WHERE customer_id = 123;
This command sets the status of the customer with ID 123 to ‘Active’.
3. Example: Updating Multiple Columns
UPDATE products
SET price = price * 1.1, last_updated = CURRENT_DATE
WHERE category = 'Electronics';
This increases the price of all electronics by 10% and updates the last modified date.
4. Using JOIN
in Updates
Snowflake supports using joins in updates to bring in values from another table:
UPDATE target
SET target.column1 = source.column1
FROM target
JOIN source ON target.id = source.id
WHERE source.status = 'Approved';
5. Best Practices
- Always back up your data before large updates.
- Test your
WHERE
condition with aSELECT
query before running the update. - Use transactions (
BEGIN
/COMMIT
/ROLLBACK
) for critical updates.
6. Common Pitfalls
- Forgetting the
WHERE
clause — this updates all rows! - Updating large tables without filtering, which can cause performance issues.
- Not checking for null values that may impact the update logic.
Conclusion
Updating data in Snowflake is straightforward when you follow best practices and understand the SQL syntax. Always test your updates on a small dataset before applying them broadly, and remember to use transactions when necessary to maintain data integrity.
MERGE
statement.