SQL UPDATE for Beginners

This article contains basic SQL UPDATE statements that beginners can use to update data in their database tables.

Update a Single Column

Here’s a basic example of the SQL UPDATE statement.

UPDATE Owners
SET LastName = 'Stallone'
WHERE OwnerId = 3;

In this case we updated the value of a single column called LastName.

The UPDATE statement starts with UPDATE, followed by the table name (i.e. the table that contains the data you want to update).

It then has the SET keyword, followed by the column you want to update and the new value, separated by an equals (=) operator.

You should always include a WHERE clause, unless you want to update all rows with the same value.

Yes, you read that right. Omitting the WHERE clause will update all rows with the same value.

Most Database Management Systems (DBMSs) have various other options that you can use with the UPDATE statement, but the ones listed here are the most commonly used ones.

Update Multiple Columns

To update multiple columns, separate each column/value pair with a comma.

UPDATE Owners
SET LastName = 'Stallone',
    Email = '[email protected]'
WHERE OwnerId = 3;

Example

In this example, we update a table.

First of all, let’s select the contents of the table.

SELECT * FROM Owners;

Result:

+-----------+-------------+------------+----------------+-------------------+
| OwnerId   | FirstName   | LastName   | Phone          | Email             |
|-----------+-------------+------------+----------------+-------------------|
| 1         | Homer       | Connery    | (308) 555-0100 | [email protected] |
| 2         | Bart        | Pitt       | (231) 465-3497 | [email protected]  |
| 3         | Nancy       | Simpson    | (489) 591-0408 | NULL              |
| 4         | Boris       | Trump      | (349) 611-8908 | NULL              |
| 5         | Woody       | Eastwood   | (308) 555-0112 | [email protected] |
+-----------+-------------+------------+----------------+-------------------+

Nancy Simpson recently got married and changed her last name, so we will update Simpson to Stallone.

Now let’s update that column, then select the table again.

UPDATE Owners
SET LastName = 'Stallone'
WHERE OwnerId = 3;

SELECT * FROM Owners;

Result:

+-----------+-------------+------------+----------------+-------------------+
| OwnerId   | FirstName   | LastName   | Phone          | Email             |
|-----------+-------------+------------+----------------+-------------------|
| 1         | Homer       | Connery    | (308) 555-0100 | [email protected] |
| 2         | Bart        | Pitt       | (231) 465-3497 | [email protected]  |
| 3         | Nancy       | Stallone   | (489) 591-0408 | NULL              |
| 4         | Boris       | Trump      | (349) 611-8908 | NULL              |
| 5         | Woody       | Eastwood   | (308) 555-0112 | [email protected] |
+-----------+-------------+------------+----------------+-------------------+

We can see that the column has been updated as specified.

Update Multiple Columns

Here’s an example of updating multiple columns.

UPDATE Owners
SET LastName = 'Biden',
    Email = '[email protected]'
WHERE OwnerId = 4;

SELECT * FROM Owners;

Result:

+-----------+-------------+------------+----------------+-------------------+
| OwnerId   | FirstName   | LastName   | Phone          | Email             |
|-----------+-------------+------------+----------------+-------------------|
| 1         | Homer       | Connery    | (308) 555-0100 | [email protected] |
| 2         | Bart        | Pitt       | (231) 465-3497 | [email protected]  |
| 3         | Nancy       | Stallone   | (489) 591-0408 | NULL              |
| 4         | Boris       | Biden      | (349) 611-8908 | [email protected] |
| 5         | Woody       | Eastwood   | (308) 555-0112 | [email protected] |
+-----------+-------------+------------+----------------+-------------------+

In this case we updated the last name and email address of owner 4.

Caution! Forgetting the WHERE Clause

The UPDATE statement can be a very dangerous statement if you don’t keep your wits about you. If you omit the WHERE clause, you will update all rows in the table.

Let’s do the previous example again, but this time we’ll forget to include the WHERE clause.

UPDATE Owners
SET LastName = 'Stallone';

SELECT * FROM Owners;

Result:

+-----------+-------------+------------+----------------+-------------------+
| OwnerId   | FirstName   | LastName   | Phone          | Email             |
|-----------+-------------+------------+----------------+-------------------|
| 1         | Homer       | Stallone   | (308) 555-0100 | [email protected] |
| 2         | Bart        | Stallone   | (231) 465-3497 | [email protected]  |
| 3         | Nancy       | Stallone   | (489) 591-0408 | NULL              |
| 4         | Boris       | Stallone   | (349) 611-8908 | NULL              |
| 5         | Woody       | Stallone   | (308) 555-0112 | [email protected] |
+-----------+-------------+------------+----------------+-------------------+

Oops! Now everyone’s last name is Stallone!

There may be times where you do in fact intend to update all rows in the table, but those times are usually quite rare.

When you’re running ad hoc queries, you might want to run a quick SELECT statement that uses the same condition of your UPDATE operation before actually running the UPDATE operation.

SELECT * FROM Owners
WHERE OwnerId = 4;

Result:

+-----------+-------------+------------+----------------+-------------------+
| OwnerId   | FirstName   | LastName   | Phone          | Email             |
|-----------+-------------+------------+----------------+-------------------|
| 4         | Boris       | Biden      | (349) 611-8908 | [email protected] |
+-----------+-------------+------------+----------------+-------------------+

That shows us the exact row that will be updated. Once we’re satisfied that it’s returning the correct row, we can go ahead with and use that same WHERE clause in the UPDATE statement.