Querydrill

Basics Updated November 2026

UPDATE

UPDATE table SET col = value WHERE condition changes col in every row that matches the condition: UPDATE products SET price = price * 1.1 WHERE category = 'Drinks' raises drink prices by 10 percent. Without WHERE every row in the table is updated, so always check the WHERE with a SELECT first.

Syntax

UPDATE table SET column1 = value1 [, column2 = value2] [FROM other] WHERE condition;

Examples

UPDATE orders SET status = 'shipped' WHERE id = 42;

Result: 1 row updated

UPDATE products SET price = price * 1.10, updated_at = CURRENT_DATE WHERE category = 'Drinks';

Result: All drink prices raised 10 percent, with the date stamped

The right side can reference the column's current value.

UPDATE customers
SET total_spent = t.spent
FROM (SELECT customer_id, SUM(amount) AS spent FROM orders GROUP BY customer_id) AS t
WHERE t.customer_id = customers.id;

Result: Each customer's total copied from the orders table

UPDATE ... FROM works in PostgreSQL and SQLite 3.33+. MySQL uses UPDATE a JOIN b SET ... instead.

Common mistakes

  • Running UPDATE without WHERE changes every row; run SELECT COUNT(*) FROM table WHERE ... with the same condition first, or wrap the update in a transaction.
  • Using WHERE col = NULL to target missing values updates nothing; use IS NULL.
  • Separating SET assignments with AND instead of commas (SET a = 1 AND b = 2) is parsed as one boolean expression: SQLite sets a to 0 or 1 and PostgreSQL raises a type error.

Practice it

The drills on Querydrill check your answer on live data, free, no account needed.

Start practicing

Questions

How do I update one table using values from another?

PostgreSQL and SQLite: UPDATE a SET col = b.col FROM b WHERE b.id = a.id. Alternatively use a correlated subquery: SET col = (SELECT col FROM b WHERE b.id = a.id).

Can I undo an UPDATE?

Only if it ran inside a transaction that has not been committed: BEGIN; UPDATE ...; check; then COMMIT or ROLLBACK. After COMMIT you need a backup.

Related

  • INSERT INTOINSERT INTO adds rows to a table. Syntax with a column list, inserting multiple rows in one statement, INSERT ... SELECT to copy data, RETURNING the new id.
  • DELETEDELETE FROM table WHERE condition removes matching rows. Delete by id, by subquery, duplicates, everything in a table, and DELETE versus TRUNCATE.
  • WHEREUse WHERE to keep only the rows that match a condition: equals, greater than, text matches, NULL checks, and combinations with AND, OR and NOT.
  • CASE WHENCASE WHEN returns different values depending on conditions. Label rows, bucket numbers into ranges, pivot with SUM(CASE ...), sort custom orders, handle NULL.

Previous: INSERT INTONext: DELETE