What Is the SQL UPDATE Statement?

The SQL UPDATE statement modifies existing rows in a table. Use UPDATE table_name SET column = value WHERE condition; to change only the rows you target. Always back up data, test with a SELECT, and include a WHERE clause to avoid updating every row.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
SQL UPDATE changes data in place. Combine SET to define new values and WHERE to target rows. Omitting WHERE updates every row, so test with SELECT first and back up critical tables.

What does the SQL UPDATE statement do?

The SQL UPDATE statement changes data in existing rows. It keeps the row structure but overwrites specified columns with new values.

What is the basic UPDATE syntax?

Use UPDATE table_name SET column1 = value1 [, column2 = value2] WHERE condition;. Without a WHERE clause, every row is updated.

Why is the WHERE clause essential in UPDATE?

The WHERE clause filters rows. Omitting it causes a full-table update, which can corrupt data. Always confirm your filter with a SELECT first.

How to update multiple columns at once?

Separate each column assignment with commas in the SET list. The WHERE clause still targets the same rows for all assignments.

Can I use expressions in SET assignments?

Yes. You can reference constants, functions, or other columns, e.g., SET price = price * 1.1, updated_at = NOW().

How to UPDATE using data from another table?

Join the target table with the source. Syntax varies by vendor. In ANSI SQL you write UPDATE t1 SET col = s.col FROM t1 JOIN source s ON t1.id = s.id;.

What about correlated subqueries in UPDATE?

Use a subquery that returns a single value per row. Example: SET balance = (SELECT SUM(amount) FROM txn WHERE txn.user_id = user.id).

How to limit the number of rows updated?

Some databases allow UPDATE ... LIMIT n (MySQL). Others require a subquery with primary keys plus ORDER BY.

How to preview changes safely?

Run the WHERE clause in a SELECT first. If results look correct, run UPDATE. Wrap long updates in a transaction and COMMIT when verified.

When should I use a transaction for UPDATE?

Use transactions for large or critical updates. A ROLLBACK restores the original state if something goes wrong.

How to audit changes made by UPDATE?

Create audit tables, use triggers, or enable database change-tracking features. Always store old and new values for regulatory compliance.

What performance considerations exist?

Indexes on WHERE columns speed updates. Batch large updates, avoid updating indexed columns unnecessarily, and monitor lock contention.

How to use the RETURNING clause to get changed rows?

PostgreSQL and Oracle let you append RETURNING * to output the affected rows without running a separate SELECT.

When to use UPSERT (INSERT ... ON CONFLICT) instead of UPDATE?

Use UPSERT when you might insert new rows if they don’t exist. UPDATE only modifies existing rows.

What are best practices for SQL UPDATE?

Back up data, test with SELECT, always include WHERE, wrap large changes in transactions, and document your queries in a tool like Galaxy for team sharing.

How does Galaxy help with UPDATE queries?

Galaxy’s AI copilot reviews your UPDATE for missing WHERE clauses, suggests index usage, and stores endorsed versions in Collections so teams avoid accidents.

What are the key takeaways?

UPDATE changes data in place, WHERE protects rows, transactions enable safe rollbacks, and modern editors like Galaxy streamline collaboration and review.

Frequently Asked Questions (FAQs)

Does UPDATE lock an entire table?

Locking depends on your DBMS. Most engines use row-level locks, but full-table locks can occur when no index supports the WHERE clause.

Can I UPDATE and SELECT in one statement?

PostgreSQL and Oracle support RETURNING to output updated rows. Other databases require a separate SELECT after the UPDATE.

How do I undo an accidental UPDATE?

If autocommit was off, issue ROLLBACK. Otherwise, restore from backups or point-in-time recovery features like PostgreSQL WAL or MySQL binlog.

Is UPDATE slower than INSERT?

Updates can be slower because they must locate existing rows and maintain indexes. Good indexing and batching keep performance acceptable.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
Truvideo Logo