SQL UPDATE Statement

Summary: in this tutorial, you will learn how to use SQL UPDATE statement to modify existing data in a table.

SQL UPDATE syntax

The UPDATE statement changes existing data in one or more rows in a table. The following illustrates the syntax of the UPDATE statement:

To update data in a table, you need to:

  • First, specify the table name that you want to change data in the UPDATE clause.
  • Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,).
  • Third, specify which rows you want to update in the WHERE clause. The WHERE clause is optional. If you omit the WHERE clause, all rows in the table will be updated.

The database engine issues a message specifying the number of affected rows after you execute the statement.

SQL UPDATE statement examples

Let’s take a look at some examples of using UPDATE statement with the employees table:

Employees Table

SQL UPDATE one column example

Suppose Janet, who has employee id 3, gets married so that you need to change her last name in the employees table.

The record of Janet in the employees before update is as follows:

Janet Record

The following statement changes Janet’s last name from Levering to Hill:

Execute the SELECT statement above again to verify the change:

Janet Record After Update

SQL UPDATE multiple columns example

For example, Janet moved to a new house therefore her address changed. Now, you have to change it in the employees table by using the following statement:

SQL UPDATE multiple rows example

The following UPDATE statement increases the salary by 2% for employees whose salary is less than $2000:

Thee employees whose salary is less than 2K received the promotion.

SQL UPDATE from SELECT

The following query selects sales person who has was in charge of more than 100 orders:

SQL UPDATE - sales person with more than 100 orders

The following query increases salary of the best sale persons by 5%. The best sale person ids are provided by a subquery.

In this tutorial, we have shown you how to use the SQL UPDATE statement to change data in one or more rows in a table.