LESSON 35 / 62 · Updating dataUpdating 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_id | status | member_name | birthday |
|---|---|---|---|
| 1 | father | Headley Quincey | 1960-05-13T00:00:00.000Z |
| 2 | mother | Flavia Quincey | 1963-02-16T00:00:00.000Z |
| 3 | son | Andie QuinceyAndie Anthony | 1983-06-05T00:00:00.000Z |
| 4 | daughter | Lela Quincey | 1985-06-07T00:00:00.000Z |
| 54 more rows | daughter | Annie Quincey | 1988-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_id | date | family_member | good | amount | unit_price |
|---|---|---|---|---|---|
| 1 | 2005-02-12T00:00:00.000Z | 1 | 1 | 1 | 2000 |
| 2 | 2005-03-23T00:00:00.000Z | 2 | 1 | 1 | 2100 |
| 3 | 2005-05-14T00:00:00.000Z | 3 | 4 | 54 | 2025 |
| 4 | 2005-07-22T00:00:00.000Z | 4 | 5 | 1 | 350 |
| 524 more rows | 2005-07-26T00:00:00.000Z | 4 | 7 | 2 | 150 |
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 clauseYou 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.
Write a query and run it to see the results.
Write a query and run it to see the results.
Write a query and run it to see the results.