SQL Standard

SQL Update With Join

One of the common DML commands in SQL is UPDATE. This command allows you to update existing records in a database table.

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:

UPDATE target_name [[AS] alias]
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:

UPDATE rental SET staff_id = 2 WHERE rental_id = 5;

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:

UPDATE table1 SET column_value (
    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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list