How to Use UPDATE in SQL

SQL’s UPDATE statement changes data inside existing rows. Supply the target table, assign new values in a SET clause, and filter affected rows with WHERE. Omitting WHERE updates every row. JOINs let you pull values from other tables, subqueries enable calculated updates, and wrapping the statement in a transaction prevents accidental mass changes.

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.
UPDATE in SQL modifies existing rows. Syntax: UPDATE table SET column = value [, …] WHERE condition; add JOIN or subquery for advanced updates. Without WHERE every row changes, so use transactions and backups to stay safe.

How to Use UPDATE in SQL

SQL’s UPDATE statement changes data in existing rows. Provide the table, set new column values, and filter rows with WHERE. Without WHERE every row updates. Transactions and backups protect you from mistakes.

What Does the SQL UPDATE Statement Do?

UPDATE replaces current column values in existing rows with the values you specify. No new rows are created and no rows are removed; only the data inside chosen rows changes.

What Is the Basic UPDATE Syntax?

Standard syntax: UPDATE <table> SET col1 = val1 [, col2 = val2] WHERE condition; The SET list defines new values, and the WHERE clause limits which rows update.

Why Is the WHERE Clause Crucial?

WHERE acts as a safety filter. Without it, every row in the target table updates, often leading to catastrophic data loss. Always double-check the WHERE before running UPDATE.

How Do I Update a Single Row?

Identify the row uniquely, typically with a primary-key column. Example: UPDATE users SET email = 'new@x.com' WHERE id = 42; Exactly one row changes.

How Can I Update Multiple Rows at Once?

Use a WHERE condition that matches several rows. Example: UPDATE orders SET status = 'shipped' WHERE ship_date < CURRENT_DATE; All qualifying rows update in a single statement.

Can I Update Based on Another Table?

Yes. Use an UPDATE … FROM or JOIN syntax (engine-specific) to pull values from a related table. This avoids multi-statement loops and boosts performance.

How Do Subquery Updates Work?

Place a scalar subquery in the SET list or WHERE clause. Example: SET total = (SELECT SUM(price) FROM items WHERE items.order_id = orders.id). The subquery runs per updated row.

When Should I Use Transactions?

Wrap UPDATE statements in BEGIN/COMMIT blocks whenever many rows are at risk. A ROLLBACK instantly reverts changes if results are not as expected.

What Are Safe Update Practices?

Always run a SELECT with the same WHERE first, keep recent backups, enable row counts, and test in staging before production. Use LIMIT or TOP for staged updates when supported.

How Do I Update JSON or Array Columns?

Databases expose functions like jsonb_set (PostgreSQL) or JSON_REPLACE (MySQL). Call these inside SET to change only the JSON path you need.

Can I Return Updated Rows?

Some databases support RETURNING (PostgreSQL), OUTPUT (SQL Server), or RETURNING INTO (Oracle) to fetch changed rows right after the UPDATE without a second query.

How Do I Undo an Accidental UPDATE?

If a transaction is still open, issue ROLLBACK. Otherwise restore from backup or use point-in-time recovery. Keeping audit tables makes targeted reversions possible.

What Performance Tips Matter for UPDATE?

Update only the columns that must change, ensure indexes exist on the WHERE filter, batch very large updates, and avoid locking the entire table by committing frequently.

Why Might UPDATE Fail?

Common causes: missing privileges, foreign-key violations, data-type mismatches, unique constraint conflicts, or deadlocks when concurrent transactions touch the same rows.

How Does UPDATE Interact With Triggers?

BEFORE and AFTER UPDATE triggers fire automatically. They can validate input, cascade changes, or write audit logs. Factor their overhead into performance planning.

How Can Galaxy Help With Updates?

Galaxy’s AI copilot previews affected rows, suggests safe WHERE clauses, and auto-rolls back in scratch runs. The desktop editor visualizes row counts instantly, reducing update anxiety.

Key Takeaways on UPDATE

Use WHERE filters, test with SELECT first, wrap in transactions, leverage JOINs for multi-table changes, and rely on tooling like Galaxy to stay safe and productive.

Frequently Asked Questions (FAQs)

Does UPDATE create a new row?

No. UPDATE only changes existing rows. Use INSERT if you need new rows.

How can I see how many rows were updated?

Most drivers return an affected-row count. CLI tools like psql echo UPDATE 42 after execution.

Is UPDATE slower than INSERT?

Usually, yes, because UPDATE must locate existing rows and may trigger more indexes and constraints.

Can I chain multiple UPDATE statements?

Yes. Execute them within a single transaction so either all succeed or all roll back.

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