Alternative for UPDATE with JOIN

Translation specification for UPDATE statement with WHERE and JOIN clauses

circle-info

Important Notice: Migration of Documentation Website

Please be advised that our documentation website is currently undergoing a migration to a new platform. To ensure you have access to the most up-to-date information, we kindly request that you visit our new documentation website located at:

Official Snowflake Snowconvert Documentationarrow-up-right

For any immediate assistance or if you encounter any issues, please contact our support team at [email protected]envelope.

Thank you for your understanding.

triangle-exclamation

Description

The pattern UPDATE FROM is used to update data based on data from other tables. This SQLServer documentationarrow-up-right provides a simple sample.

Review the following SQL Server syntax from the documentationarrow-up-right.

SQL Server Syntax

UPDATE [table_name] 
SET column_name = expression [, ...]
[FROM <table_source> [, ...]]
[WHERE <search_condition>]
[OPTION (query_hint)]
  • table_name: The table or view you are updating.

  • SET: Specifies the columns and their new values. The SET clause assigns a new value (or expression) to one or more columns.

  • FROM: Used to specify one or more source tables (like a join). It helps define where the data comes from to perform the update.

  • WHERE: Specifies which rows should be updated based on the condition(s). Without this clause, all rows in the table would be updated.

  • OPTION (query_hint): Specifies hints for query optimization.

Snowflake syntax

The Snowflake syntax can also be reviewed in the Snowflake documentationarrow-up-right.

circle-info

Snowflake does not support JOINs in UPDATE clause.

Required parameters

  • target_table:Specifies the table to update.

  • col_name:Specifies the name of a column in target_table. Do not include the table name. E.g., UPDATE t1 SET t1.col = 1 is invalid.

  • value:Specifies the new value to set in col_name.

Optional parameters

  • FROM`` additional_tables: Specifies one or more tables to use for selecting rows to update or for setting new values. Note that repeating the target table results in a self-join.

  • WHERE`` condition:The expression that specifies the rows in the target table to update. Default: No value (all rows of the target table are updated)

Translation Summary

SQL Server JOIN type
Snowflake Best Alternative

Single INNER JOIN

Use the target table in the FROM clause to emulate an INNER JOIN.

Multiple INNER JOIN

Use the target table in the FROM clause to emulate an INNER JOIN.

Multiple INNER JOIN + Agregate condition

Use subquery + IN Operation

Single LEFT JOIN

Use subquery + IN Operation

Multiple LEFT JOIN

Use Snowflake UPDATE reordering the statements as needed. UPDATE [target_table_name]

SET [all_set_statements]

FROM [all_left_join_tables_separated_by_comma]

WHERE [all_clauses_into_the_ON_part]

Multiple RIGHT JOIN

Use Snowflake UPDATE reordering the statements as needed. UPDATE [target_table_name]

SET [all_set_statements]

FROM [all_right_join_tables_separated_by_comma]

WHERE [all_clauses_into_the_ON_part]

Single RIGHT JOIN

Use the table in the FROM clause and add filters in the WHERE clause as needed.

Note-1: Simple JOIN may use the table in the FROM clause and add filters in the WHERE clause as needed.

Note-2: Other approaches may include (+) operand to define the JOINs.

Sample Source Patterns

Setup data

chevron-rightData Insertion for sampleshashtag

Case 1: Single INNER JOIN Update

For INNER JOIN, if the table is used inside the FROM statements, it automatically turns into INNER JOIN. Notice that there are several approaches to support JOINs in UPDATE statements in Snowflake. This is one of the simplest patterns to ensure readability.

SQL Server

Snowflake

Other approaches:

chevron-rightMERGE INTOhashtag
chevron-rightIN Operationhashtag

Case 2: Multiple INNER JOIN Update

SQL Server

Snowflake

Case 3: Multiple INNER JOIN Update with Aggregate Condition

SQL Server

Snowflake

Case 4: Single LEFT JOIN Update

SQL Server

Snowflake

circle-info

This approach in Snowflake will not work because it does not update the necessary rows:

UPDATE Orders O SET O.Quantity = 13 FROM Customers C WHERE O.CustomerID = C.CustomerID AND C.CustomerID IS NULL AND O.ProductID = 13;

Case 5: Multiple LEFT JOIN and RIGHT JOIN Update

This is a more complex pattern. To translate multiple LEFT JOINs, please review the following pattern:

circle-info

LEFT JOIN and RIGHT JOIN will depend on the order in the FROM clause.

SQL Server

Snowflake

Case 6: Mixed INNER JOIN and LEFT JOIN Update

SQL Server

Snowflake

Case 7: Single RIGHT JOIN Update

SQL Server

Snowflake

Know Issues

  • Since UPDATE in Snowflake does not allow the usage of JOINs directly, there may be cases that do not match the patterns described.

Last updated