SQL UPDATE: change existing data safely, step by step

UPDATE changes values in rows that already exist: an order ships, a price goes up, a stock count gets corrected. SET says what changes, WHERE says where. Leave out WHERE and UPDATE changes every row in the table.

In short: UPDATE products SET price = 5.49 WHERE product_id = 1; sets the price of product 1 to 5.49. Separate several columns with commas after SET. The right-hand side may use the current value: SET stock = stock + 20.

Before you start: conditions with WHERE and adding rows with INSERT.

Try it now

Every run starts from fresh data. After Run, the result panel lists every row your statement changed, so you see straight away if it touched more rows than you meant.

Task Order 1016 went out today, handled by David Ross (employee_id 8). In the orders table, set this order's status to shipped and its employee_id to 8. After Run or Check you will see every row that changed.

orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL

How UPDATE works

UPDATE names the table, SET the new values and WHERE the rows. Just like with SELECT, the database goes through the table, tests the condition, and writes the new values into every row that matches. Every other column and row stays as it was.

UPDATE products
SET stock = stock + 20
WHERE product_id = 4;

Twenty Comfort office chairs have arrived. The right-hand side reads the old stock of 12, the database adds 20 and stores 32. You don't need to know the old value, and the statement is still right if someone recounted in the meantime.

SELECT first, then UPDATE

Before you change anything in a real database, run the same condition as a SELECT. It shows you exactly the rows the UPDATE is going to hit:

SELECT product_id, name, price
FROM products
WHERE category = 'Printer supplies';
product_idnameprice
6Black ink cartridge24.9
11Black toner XL89.9

Two rows, the right two. Now swap the first line and keep the WHERE: UPDATE products SET price = ROUND(price * 0.9, 2) WHERE category = 'Printer supplies'; cuts both prices by 10%, to 22.41 and 80.91. Copy the condition rather than retyping it, so nothing changes on the way.

Update multiple columns

SET can hold as many assignments as you need, separated by commas: SET status = 'shipped', employee_id = 8. They all apply to the same rows.

Practice: a 5% price increase

Task The supplier is raising its paper prices. In products, increase the price of every product in the Paper category by 5% and round the result to two decimal places with ROUND. All other products stay as they are.

products 12 rows, 5 columns
product_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

UPDATE with a subquery

Often the new value or the condition lives in another table. Then a subquery in parentheses goes after SET or into WHERE. A subquery after SET may refer to the row being updated and has to return exactly one value. The lesson on subqueries covers how they work in general.

Task Before shipping, every open order should be billed at the current list price. In order_items, set unit_price to the product's price from products, but only for items that belong to orders whose status in orders is open. After Run or Check you will see the items whose price actually changed.

order_items 38 rows, 4 columns
order_idproduct_idquantityunit_price
10011404.79
10013252.79
10016424.9
1002223.49
10021035.49
10031604.69
100311684.9
10033502.59
100443179
100493269
100551449
10061304.79
10067106.99
100782219
100711489.9
10071204.99
100846175
1008253.49
1009101204.99
10092403.19
100992279
1010726.99
1010213.49
1011121129
10111104.99
10113122.79
10126823.9
10121404.69
101391279
101452439
101442179
10151504.79
101511289.9
10161045.49
1017233.49
1017726.99
10181804.79
101861024.9
products 12 rows, 5 columns
product_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290
orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

Common mistakes

Forgetting WHERE

UPDATE products SET stock = 0; sets the stock of all twelve products to zero. There is no warning; the database does exactly what you wrote. In the first exercise the check then says "After your changes: The check shows 18 rows" when a single one is wanted, and the result panel lists all 18 orders you changed. In a real database, only a backup or an open transaction gets you out of that.

AND instead of a comma

SET status = 'shipped' AND employee_id = 8 looks sensible but is a single assignment: status receives the result of the comparison 'shipped' AND employee_id = 8, which is 0. The exercise reports that not every value matches. Separate columns with commas.

A condition that is too broad

WHERE status = 'open' instead of WHERE order_id = 1016 hits all three open orders: the check reports "The check shows 3 rows" when one is wanted. Only the primary key is guaranteed to pick out a single record.

Breaking a foreign key

Set employee_id to 99 and you point at an employee who does not exist. employee_id is a foreign key to the employees table, so the database refuses the UPDATE and the exercise reports a foreign key error. The database keeps references valid when you change rows, too.

Typing in values

Copying the IDs of the paper products, or working out the new price yourself and typing the number, only solves the task for today. That is why the check also runs on different data and says: "The result is right with this data but not with other data."

UPDATE in MySQL, PostgreSQL, SQL Server and SQLite

UPDATE … SET … WHERE and subqueries work the same everywhere. For updates that take values from a second table, each database also has its own JOIN or FROM syntax, because the SQL standard does not define one. The example sets open order 1018 to list prices.

DatabaseSyntax
MySQLUPDATE order_items JOIN products ON products.product_id = order_items.product_id SET order_items.unit_price = products.price WHERE order_items.order_id = 1018; The join sits between UPDATE and SET. ORDER BY and LIMIT are not allowed in this form.
PostgreSQLUPDATE order_items SET unit_price = p.price FROM products p WHERE p.product_id = order_items.product_id AND order_items.order_id = 1018; The docs warn that if a row matches several rows of the other table, which one is used is not predictable.
SQL ServerUPDATE oi SET unit_price = p.price FROM order_items AS oi JOIN products AS p ON p.product_id = oi.product_id WHERE oi.order_id = 1018; The target table is listed in FROM as well.
SQLite (these exercises)Like PostgreSQL, with UPDATE … FROM, since version 3.33.0. The exercise above accepts that form too.

A safety net for a forgotten WHERE: the MySQL command-line client has a --safe-updates option. With it, UPDATE and DELETE fail unless WHERE uses a key column or a LIMIT is given.

FAQ

Can I undo an UPDATE?

Only while it runs inside a transaction that has not been committed yet (ROLLBACK), or from a backup. That is why you check with SELECT first. More in the lesson on transactions.

How do I update multiple columns in SQL?

Separate them with commas after SET: SET status = 'shipped', employee_id = 8.

How do I increase a value by a percentage?

Calculate from the current value: SET price = ROUND(price * 1.05, 2) for 5% more, price * 0.9 for 10% less.

Why does it say "5 rows changed" when only one value changed?

The database counts every row WHERE matches, even if the new value equals the old one. In the subquery exercise, four of the open items already had the current price.

How do I clear a field?

With SET phone = NULL. A plain equals sign is right here, because this is an assignment, not a comparison. Only in WHERE do you write IS NULL.

Next up: DELETE

Some rows need to go entirely: a dropped item, cancelled orders. DELETE removes rows, and you will see why foreign keys sometimes say no.

Practice DELETE

Write to us

Choose language

Select your preferred language