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_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
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_id | name | price |
|---|---|---|
| 6 | Black ink cartridge | 24.9 |
| 11 | Black toner XL | 89.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_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
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_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
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.
| Database | Syntax |
|---|---|
| MySQL | UPDATE 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. |
| PostgreSQL | UPDATE 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 Server | UPDATE 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.