SQL UPDATE statement

The SQL UPDATE statement is used to update one or more rows in a table. Here is the syntax:

UPDATE table_name SET column_name1=’value1′, column_name2=’value2′ WHERE column_name3=’some_value’;

We will use the Employee table for our example:

+----------------+-----------+-----------+-----------+---------------------------------+
| employeeNumber | lastName  | firstName | extension | email                           |
+----------------+-----------+-----------+-----------+---------------------------------+
|              0 | Doe       | John      | x233333   |                                 |
|           1002 | Murphy    | Diane     | x5800     | dmurphy@classicmodelcars.com    |
|           1056 | Patterson | Mary      | x4611     | mpatterso@classicmodelcars.com  |
|           1076 | Firrelli  | Jeff      | x9273     | jfirrelli@classicmodelcars.com  |
|           1088 | Patterson | William   | x4871     | wpatterson@classicmodelcars.com |
|           1102 | Bondur    | Gerard    | x5408     | gbondur@classicmodelcars.com    |
+----------------+-----------+-----------+-----------+---------------------------------+

Notice how the employee John Doe has no e-mail address. To update his email record, the following command can be used:

UPDATE employee SET email=’john@email.com’ WHERE firstName=’John’ AND lastName=’Doe’;

This would produce the following result:

+----------------+-----------+-----------+-----------+---------------------------------+
| employeeNumber | lastName  | firstName | extension | email                           |
+----------------+-----------+-----------+-----------+---------------------------------+
|              0 | Doe       | John      | x233333   | john@email.com                  |
|           1002 | Murphy    | Diane     | x5800     | dmurphy@classicmodelcars.com    |
|           1056 | Patterson | Mary      | x4611     | mpatterso@classicmodelcars.com  |
|           1076 | Firrelli  | Jeff      | x9273     | jfirrelli@classicmodelcars.com  |
|           1088 | Patterson | William   | x4871     | wpatterson@classicmodelcars.com |
|           1102 | Bondur    | Gerard    | x5408     | gbondur@classicmodelcars.com    |
+----------------+-----------+-----------+-----------+---------------------------------+
Geek University 2022