Translation specification for UPDATE statement with WHERE and JOIN clauses
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:
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
Data Insertion for samples
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
CustomerID
Quantity
CustomerName
1
10
John Doe
Snowflake
CustomerID
Quantity
CustomerName
1
10
John Doe
Other approaches:
MERGE INTOIN Operation
Case 2: Multiple INNER JOIN Update
SQL Server
CustomerID
Quantity
CustomerName
3
5
Alice Johnson
Snowflake
CustomerID
Quantity
CustomerName
3
5
Alice Johnson
Case 3: Multiple INNER JOIN Update with Aggregate Condition
SQL Server
CustomerID
CustomerName
Quantity
Price
11
Jack Grey
6
29.99
18
Quincy Brown
6
15.99
20
Sam Green
6
89.99
22
Ursula Red
6
9.99
24
Wendy Black
6
49.99
Snowflake
CustomerID
CustomerName
Quantity
Price
11
Jack Grey
6
29.99
18
Quincy Brown
6
15.99
20
Sam Green
6
89.99
22
Ursula Red
6
9.99
24
Wendy Black
6
49.99
Case 4: Single LEFT JOIN Update
SQL Server
OrderID
CustomerID
ProductID
Quantity
OrderDate
5
null
5
7
2024-11-05
13
null
13
13
2024-11-13
Snowflake
OrderID
CustomerID
ProductID
Quantity
OrderDate
5
null
5
7
2024-11-05
13
null
13
13
2024-11-13
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:
LEFT JOIN and RIGHT JOIN will depend on the order in the FROM clause.
SQL Server
OrderID
CustomerID
ProductID
Quantity
OrderDate
3
3
3
3
2024-11-12
Snowflake
OrderID
CustomerID
ProductID
Quantity
OrderDate
3
3
3
3
2024-11-12
Case 6: Mixed INNER JOIN and LEFT JOIN Update
SQL Server
CustomerID
CustomerName
Quantity
null
null
4
Snowflake
CustomerID
CustomerName
Quantity
null
null
4
Case 7: Single RIGHT JOIN Update
SQL Server
OrderID
CustomerID
ProductID
Quantity
CustomerName
3
3
3
1000
Alice Johnson
Snowflake
OrderID
CustomerID
ProductID
Quantity
CustomerName
3
3
3
1000
Alice Johnson
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.
UPDATE Orders
SET Quantity = 10
FROM Orders O
INNER JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE C.CustomerName = 'John Doe';
-- Select the changes
SELECT Orders.CustomerID, Orders.Quantity, Customers.CustomerName
FROM Orders, Customers
WHERE Orders.CustomerID = Customers.CustomerID
AND Customers.CustomerName = 'John Doe';
UPDATE Orders O
SET O.Quantity = 10
FROM
Customers C
WHERE
C.CustomerName = 'John Doe'
AND O.CustomerID = C.CustomerID;
-- Select the changes
SELECT Orders.CustomerID, Orders.Quantity, Customers.CustomerName
FROM Orders, Customers
WHERE Orders.CustomerID = Customers.CustomerID
AND Customers.CustomerName = 'John Doe';
MERGE INTO Orders O
USING Customers C
ON O.CustomerID = C.CustomerID
WHEN MATCHED AND C.CustomerName = 'John Doe' THEN
UPDATE SET O.Quantity = 10;
UPDATE Orders O
SET O.Quantity = 10
WHERE O.CustomerID IN
(SELECT CustomerID FROM Customers WHERE CustomerName = 'John Doe');
UPDATE Orders
SET Quantity = 5
FROM Orders O
INNER JOIN Customers C ON O.CustomerID = C.CustomerID
INNER JOIN Products P ON O.ProductID = P.ProductID
WHERE C.CustomerName = 'Alice Johnson' AND P.ProductName = 'Tablet';
-- Select the changes
SELECT Orders.CustomerID, Orders.Quantity, Customers.CustomerName FROM Orders, Customers
WHERE Orders.CustomerID = Customers.CustomerID
AND Customers.CustomerName = 'Alice Johnson';
UPDATE Orders O
SET O.Quantity = 5
FROM Customers C, Products P
WHERE O.CustomerID = C.CustomerID
AND C.CustomerName = 'Alice Johnson'
AND P.ProductName = 'Tablet'
AND O.ProductID = P.ProductID;
-- Select the changes
SELECT Orders.CustomerID, Orders.Quantity, Customers.CustomerName FROM Orders, Customers
WHERE Orders.CustomerID = Customers.CustomerID
AND Customers.CustomerName = 'Alice Johnson';
UPDATE Orders
SET Quantity = 6
FROM Orders O
INNER JOIN Customers C ON O.CustomerID = C.CustomerID
INNER JOIN Products P ON O.ProductID = P.ProductID
WHERE C.CustomerID IN (SELECT CustomerID FROM Orders WHERE Quantity > 3)
AND P.Price < 200;
SELECT C.CustomerID, C.CustomerName, O.Quantity, P.Price FROM Orders O
INNER JOIN Customers C ON O.CustomerID = C.CustomerID
INNER JOIN Products P ON O.ProductID = P.ProductID
WHERE C.CustomerID IN (SELECT CustomerID FROM Orders WHERE Quantity > 3)
AND P.Price < 200;
UPDATE Orders O
SET Quantity = 6
WHERE O.CustomerID IN (SELECT CustomerID FROM Orders WHERE Quantity > 3)
AND O.ProductID IN (SELECT ProductID FROM Products WHERE Price < 200);
-- Select changes
SELECT C.CustomerID, C.CustomerName, O.Quantity, P.Price FROM Orders O
INNER JOIN Customers C ON O.CustomerID = C.CustomerID
INNER JOIN Products P ON O.ProductID = P.ProductID
WHERE C.CustomerID IN (SELECT CustomerID FROM Orders WHERE Quantity > 3)
AND P.Price < 200;
UPDATE Orders
SET Quantity = 13
FROM Orders O
LEFT JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE C.CustomerID IS NULL AND O.ProductID = 13;
-- Select the changes
SELECT * FROM orders
WHERE CustomerID IS NULL;
UPDATE Orders
SET Quantity = 13
WHERE OrderID IN (
SELECT O.OrderID
FROM Orders O
LEFT JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE C.CustomerID IS NULL AND O.ProductID = 13
);
-- Select the changes
SELECT * FROM orders
WHERE CustomerID IS NULL;
UPDATE [target_table_name]
SET [all_set_statements]
FROM [all_left_join_tables_separated_by_comma]
WHERE [all_clauses_into_the_ON_part]
UPDATE Orders
SET
Quantity = C.CustomerID
FROM Orders O
LEFT JOIN Customers C ON C.CustomerID = O.CustomerID
LEFT JOIN Products P ON P.ProductID = O.ProductID
WHERE C.CustomerName = 'Alice Johnson'
AND P.ProductName = 'Tablet';
SELECT O.OrderID, O.CustomerID, O.ProductID, O.Quantity, O.OrderDate
FROM Orders O
LEFT JOIN Customers C ON C.CustomerID = O.CustomerID
LEFT JOIN Products P ON P.ProductID = O.ProductID
WHERE C.CustomerName = 'Alice Johnson'
AND P.ProductName = 'Tablet';
UPDATE Orders O
SET O.Quantity = C.CustomerID
FROM Customers C, Products P
WHERE O.CustomerID = C.CustomerID
AND C.CustomerName = 'Alice Johnson'
AND P.ProductName = 'Tablet'
AND O.ProductID = P.ProductID;
SELECT O.OrderID, O.CustomerID, O.ProductID, O.Quantity, O.OrderDate
FROM Orders O
LEFT JOIN Customers C ON C.CustomerID = O.CustomerID
LEFT JOIN Products P ON P.ProductID = O.ProductID
WHERE C.CustomerName = 'Alice Johnson'
AND P.ProductName = 'Tablet';
UPDATE Orders
SET Quantity = 4
FROM Orders O
INNER JOIN Products P ON O.ProductID = P.ProductID
LEFT JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE C.CustomerID IS NULL AND P.ProductName = 'Monitor';
-- Select changes
SELECT O.CustomerID, C.CustomerName, O.Quantity FROM Orders O
INNER JOIN Products P ON O.ProductID = P.ProductID
LEFT JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE C.CustomerID IS NULL AND P.ProductName = 'Monitor';
UPDATE Orders O
SET Quantity = 4
WHERE O.ProductID IN (SELECT ProductID FROM Products WHERE ProductName = 'Monitor')
AND O.CustomerID IS NULL;
-- Select changes
SELECT O.CustomerID, C.CustomerName, O.Quantity FROM Orders O
INNER JOIN Products P ON O.ProductID = P.ProductID
LEFT JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE C.CustomerID IS NULL AND P.ProductName = 'Monitor';
UPDATE O
SET O.Quantity = 1000
FROM Orders O
RIGHT JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE C.CustomerName = 'Alice Johnson';
-- Select changes
SELECT
O.OrderID,
O.CustomerID,
O.ProductID,
O.Quantity,
O.OrderDate,
C.CustomerName
FROM
Orders O
RIGHT JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE
C.CustomerName = 'Alice Johnson';
UPDATE Orders O
SET O.Quantity = 1000
FROM Customers C
WHERE O.CustomerID = C.CustomerID
AND C.CustomerName = 'Alice Johnson';
-- Select changes
SELECT
O.OrderID,
O.CustomerID,
O.ProductID,
O.Quantity,
O.OrderDate,
C.CustomerName
FROM
Orders O
RIGHT JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE
C.CustomerName = 'Alice Johnson';