SQL UPDATE Statement

In this tutorial, we'll learn to update rows in SQL with the help of examples.

The SQL UPDATE statement is used to edit existing rows in a database table. For example,

UPDATE Customers
SET first_name = 'Johnny'
WHERE customer_id = 1;

Here, the SQL command changes the value of the first_name column will be Johnny if customer_id is equal to 1.

How to update rows in SQL?
Example: SQL UPDATE Statement

Note: If we need to insert a new row instead of updating an existing row, we can use the SQL INSERT INTO statement.


Update Multiple Values in a Row

We can also update multiple values in a row at once. For example,

UPDATE Customers
SET first_name = 'Johnny', last_name = 'Depp'
WHERE customer_id = 1;

Here, the SQL command changes the value of the first_name column to Johnny and last_name to Depp if customer_id is equal to 1.


Update Multiple Rows

The UPDATE statement can update multiple rows at once. For example,

UPDATE Customers
SET country = 'NP'
WHERE age = 22;

Here, the SQL command changes the value of the country column to NP if age is 22. If there are more than one rows with age equals to 22, all the matching rows will be edited.


Update all Rows

We can update all the rows in a table at once by omitting the WHERE clause. For example,

UPDATE Customers
SET country = 'NP';

Here, the SQL command changes the value of the country column to NP for all rows.

Note: We should be cautious while using the UPDATE statement. If we omit the WHERE clause, all the rows will be changed and this change is irreversible.


SQL Update With JOIN

We can also use the UPDATE statement with the JOIN clause in SQL.

Take a look at the Stackoverflow thread on, How to use the UPDATE statement with JOIN?

Did you find this article helpful?