UPDATE (Update Rows)
Last updated: 2026-11-04
UPDATE (Update Rows)
UPDATE modifies rows that already exist. It picks target rows with WHERE and assigns new values with SET, where the new value may be an expression computed from the old one (say amount * 1.1). UPDATE can also read from other tables via a FROM clause and return the modified rows with RETURNING.
| Kind | command |
|---|---|
| Example Language | SQL |
📝 Syntax
UPDATE <table> [AS <alias>]
SET <column> = <expression>, ...
[FROM <other table> [, ...]]
[WHERE <condition>]
[RETURNING <column or expression>]
⚙️ Parameters
| SET | The columns to update and their new values |
|---|---|
| WHERE | Filters the rows to update |
| FROM | Other tables that supply the new values |
| RETURNING | Returns the updated rows |
Returns:The number of affected rows; with RETURNING, the updated rows are returned as well.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
DROP TABLE
CREATE TABLE
INSERT 0 3
UPDATE 2
id | customer | amount | bonus
---+----------+---------+-------
1 | Alice | 1100.00 | 100.00
2 | Bob | 550.00 | 50.00
📌 Key Points
- Within one UPDATE, every expression on the right-hand side of SET reads the old row values, never another column's new value.
- An UPDATE without WHERE rewrites the whole table — always add a predicate and check the row count with RETURNING.
- UPDATE writes a new row version for every row it touches; the old versions become dead tuples that VACUUM reclaims.
- A FROM clause lets another table supply the new values, e.g. converting amounts with a rate table.
- UPDATE fires UPDATE triggers only, never DELETE triggers.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QHow do I compute a value from the old row in a single UPDATE?
AJust write amount = amount * 1.1: the right-hand side reads the old value and the left-hand side stores the new one.
QDoes UPDATE lock the whole table?
ANot the whole table, but the updated rows take row-level locks that make concurrent updates to the same rows wait. With an index, only the matched rows are touched.