This tutorial will cover how to perform an update with join on a database table using Standard SQL commands.
SQL Update
The UPDATE command is used to update existing records within a database table. The command syntax is shown below:
SET set_clause
[FROM from_clause]
WHERE condition
set_clause ::= update_item[, ...]
update_item ::= column_name = expression
Consider the table as shown below:
We can update the previous table and set the staff_id to 2 where the rental _id = 5. An example statement is provided. below:
The previous statement should search the table where the rental_id equals 5 and update the staff_id to 5.
The resulting record is shown below:
Notice the new record reflects the updated value.
SQL Update With Joins
Standard SQL allows you to perform an UPDATE JOIN operation. Although it will mainly depend on the target join, we can express the syntax as shown below:
SELECT COLUMN(s) FROM table2
WHERE table1.column = table2.column),
condition
WHERE COLUMN IN (SELECT COLUMN(s) FROM table2)
);
The previous syntax describes how to perform an update join between two tables.
NOTE: Implementation may differ depending on the database engine.
Conclusion
In this article, we covered how to perform updates in standard SQL. We also covered the basic syntax for performing update joins. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.