RienSQL
LESSON 35 / 62 · Updating data

Updating data, UPDATE operator

How to change data in an SQL table with UPDATE: SET and WHERE syntax, updating several columns in one query, updating based on another table, common mistakes. Examples for MySQL and PostgreSQL.

Data that has been added does not always stay correct: people change their surnames, typos turn up in payments, prices need to be recalculated. To fix a row without deleting it, use the UPDATE operator.

General query structure with the UPDATE operator

UPDATE table_name
SET table_field = value [, ...]
[WHERE condition];
  • [ ]optional part
  • ...can be repeated

The column that gets a new value. You can list several table_field = value pairs separated by commas.

The new value: a number, a string, an expression like price * 1.1, or NULL.

Which rows to change. Without WHERE, every row of the table is changed.

table_name
The table whose records are changed.
condition
Optional

For example, Andie (Andie Quincey) has changed his surname and is now Andie Anthony.

member_idINTgood_idINTgood_type_idINTpayment_idINT

UPDATE FamilyMembers
SET member_name = 'Andie Anthony'
WHERE member_name = 'Andie Quincey';
member_idstatusmember_namebirthday
1fatherHeadley Quincey1960-05-13T00:00:00.000Z
2motherFlavia Quincey1963-02-16T00:00:00.000Z
3sonAndie QuinceyAndie Anthony1983-06-05T00:00:00.000Z
4daughterLela Quincey1985-06-07T00:00:00.000Z
54 more rowsdaughterAnnie Quincey1988-04-10T00:00:00.000Z

Be careful when updating data. If you skip the WHERE operator, all entries will be updated.

Changing several columns

To change several columns at once, list the table_field = value pairs after SET, separated by commas. For example, Andie bought 4 cartons of milk at 25 each, but the payment with ID 3 was recorded by mistake as 5 at 20. Let's fix both columns in one query:

UPDATE Payments
SET amount = 4, unit_price = 25
WHERE payment_id = 3;
payment_iddatefamily_membergoodamountunit_price
12005-02-12T00:00:00.000Z1112000
22005-03-23T00:00:00.000Z2112100
32005-05-14T00:00:00.000Z34542025
42005-07-22T00:00:00.000Z451350
524 more rows2005-07-26T00:00:00.000Z472150

All the listed columns change in the same rows — the ones that match the WHERE condition.

Calculated values

A new value can be calculated from the old one — for example, to double all prices:

UPDATE Payments
SET unit_price = unit_price * 2;

The expression can also use other columns of the same row, as long as their types are compatible. For example, let's record the payment with ID 3 as a single item: unit_price will hold the cost of the whole purchase, and amount will be one.

UPDATE Payments
SET unit_price = unit_price * amount,
    amount = 1
WHERE payment_id = 3;

In a single-table UPDATE, assignments in SET are performed from left to right, and the next expression already sees the new value of the column. If you swap the assignments, unit_price is multiplied by the 1 just written to amount and does not change:

UPDATE Payments
SET amount = 1,
    unit_price = unit_price * amount
WHERE payment_id = 3;

So put the assignment to a column that other expressions depend on at the end of the list.

Updating based on data from another table

Sometimes the rows to update have to be selected using data from another table. For example, it turned out that every caviar payment was recorded at half the real price. The simplest way is a subquery in WHERE:

UPDATE Payments
SET unit_price = unit_price * 2
WHERE good IN (
    SELECT good_id
    FROM Goods
    WHERE good_name LIKE '%caviar%'
);

You can do the same by joining the tables with JOIN right in the UPDATE query:

MySQL 8.4

UPDATE table_name_1
JOIN table_name_2 ON join_condition
SET table_name_1.table_field = value [, ...]
[WHERE condition];
  • [ ]optional part
  • ...can be repeated

The joined table whose data helps select the rows and calculate the new values.

How the rows are linked, for example table_name_1.field = table_name_2.field.

Which rows to change. The condition can check columns of both tables.

table_name_1
The table whose records are changed.
condition
Optional
UPDATE Payments
JOIN Goods ON Payments.good = Goods.good_id
SET Payments.unit_price = Payments.unit_price * 2
WHERE Goods.good_name LIKE '%caviar%';

A subquery cannot read the same table that UPDATE changes. For example, you cannot double the price in payments cheaper than average this way:

UPDATE Payments
SET unit_price = unit_price * 2
WHERE unit_price < (SELECT AVG(unit_price) FROM Payments);

The query fails with an error:

ERROR 1093 (HY000): You can't specify target table 'Payments' for update in FROM clause

You can work around this by wrapping the subquery in one more SELECT. The average price is then first calculated and stored in a temporary table, and the update reads from it:

UPDATE Payments
SET unit_price = unit_price * 2
WHERE unit_price < (
    SELECT avg_price
    FROM (SELECT AVG(unit_price) AS avg_price FROM Payments) AS stats
);

Common mistakes

Forgotten WHERE

A query without a condition changes every row of the table. Check the condition with a SELECT query first, and then put it into UPDATE.

AND instead of a comma in SET

The query below does not change two columns: everything to the right of the first = is treated as a single logical expression.

UPDATE Payments
SET amount = 4 AND unit_price = 25
WHERE payment_id = 3;

The query runs without an error and writes the result of this expression — 0 or 1 — to amount, while unit_price stays the same.

To change both columns, separate the assignments with a comma. That is how SET knows where one ends and the next one begins:

UPDATE Payments
SET amount = 4, unit_price = 25
WHERE payment_id = 3;

Comparing with NULL using =

The condition WHERE field = NULL matches no rows, so the query changes nothing. To check for an empty value, use IS NULL.

Test yourself

Some users in the Users table have no email confirmation date: their email_verified_at field is NULL. How many rows will this query change?

UPDATE Users
SET email_verified_at = '2024-01-01'
WHERE email_verified_at = NULL;

Python Academy

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1Updating the name
Change the name of "Wednesday Addams" to the new "Tuesday Addams".
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 2Updating the cost of all housing
Update the cost of all rooms in the table (Rooms) by adding 10 units to the current one.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 3Updating an owner's amenities
The owner George Clooney has installed a TV and internet in all of his places. Set has_tv and has_internet to true for these places. The owner's name is stored in the Users table.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.