SQL UPDATE

UPDATE changes values in existing rows. SET says what to change; WHERE says which rows. Without WHERE, every row in the table is updated.

A good habit: run the WHERE as a SELECT first to see which rows you are about to change.

Syntax

UPDATE table_name
SET col = new_value
WHERE condition;

Try it yourself

Run the example below on a real PostgreSQL database, then change it and run it again. Nothing to install, no sign-up.

update.sql · PostgreSQL
Loading editor…

Examples

1. Price increase

UPDATE products SET price = price * 1.1 WHERE category = 'Books'; SELECT name, price FROM products WHERE category = 'Books';

Books are 10% more expensive (rolled back afterwards).

Common mistakes

  • Forgetting WHERE and updating every row.

Practice: test yourself

This challenge is from our interactive course. Write the query and press Check answer.

Challenge · 20 pts

Increase the price of every product in the 'Books' category by 10%.

challenge.sql · PostgreSQL
Loading editor…

FAQ

Can I undo an UPDATE?

Only inside a transaction (BEGIN ... ROLLBACK) or from a backup. That's why our sandbox wraps everything in a transaction.

Learn SQL step by step

65 hands-on exercises, streaks, a leaderboard and a certificate.

Start free →