Lesson 34 of 12122 min read

UPDATE Statement in SQL: Updating Single and Multiple Rows with WHERE

Learn how the SQL UPDATE statement modifies existing rows, including single-row, multi-row, and multi-column updates with WHERE.

Author: CodersNexus

UPDATE Statement in SQL: Updating Single and Multiple Rows with WHERE

Data is rarely static — a customer changes their address, a product's price needs adjusting, an order's status moves from 'placed' to 'shipped.' UPDATE is the statement that modifies existing rows in place, and like DELETE, it's powerful enough that the WHERE clause controlling it deserves real care and attention.

What Is the UPDATE Statement?

UPDATE is a DML statement used to modify the values of existing rows in a table. It specifies the table to update, a SET clause listing which columns to change and their new values, and typically a WHERE clause to limit exactly which rows are affected.

What You'll Learn

  • Update a single column on specific rows using UPDATE with WHERE.
  • Update multiple columns in a single UPDATE statement.
  • Understand the risk of omitting WHERE and best practices to prevent it.
  • Use expressions within SET to update a value relative to its current value.

Key Terms to Know

  • UPDATE: A DML statement used to modify existing rows in a table.
  • SET clause: The part of an UPDATE statement specifying which columns to change and their new values.
  • Relative update: An UPDATE that sets a column based on its own current value, such as price = price * 0.9.

Basic UPDATE Syntax: Single Column, Single Row

UPDATE customers SET email = 'new.email@example.com' WHERE customer_id = 42; changes only the email column, and only for the row where customer_id equals 42. The WHERE clause here works exactly as it does in SELECT, narrowing down exactly which row (or rows) are targeted.

Updating Multiple Columns and Multiple Rows

Multiple columns can be updated in a single statement by separating them with commas in the SET clause: UPDATE customers SET email = 'new@x.com', phone = '9999999999' WHERE customer_id = 42;. If the WHERE condition matches multiple rows, every matching row gets updated with the same SET values in one statement — for example, UPDATE products SET category = 'archived' WHERE stock_qty = 0; would archive every out-of-stock product at once.

Relative Updates and the Danger of Omitting WHERE

SET can reference the column's own current value to compute a relative update, such as UPDATE products SET price = price * 1.1 WHERE category = 'electronics';, which increases every electronics product's price by 10% relative to its existing price, rather than setting it to a single fixed value.

Running UPDATE without any WHERE clause at all updates every single row in the table — this is one of the most common, costly mistakes in SQL. A strong safety habit is writing and testing the equivalent SELECT statement with the same WHERE condition first, confirming exactly which rows would be affected, before converting it to an UPDATE.

Visual Summary

Picture UPDATE as a worker walking down a row of filing folders with a correction pen. WHERE tells the worker exactly which folders to open and correct — skip everyone else. Without WHERE, the worker is instructed to open and correct every single folder in the entire cabinet, whether it needed correcting or not.

UPDATE Statement Components

ClausePurposeExample
UPDATE table_nameSpecifies which table to modifyUPDATE products
SET col = valueSpecifies new value(s) for column(s)SET price = 99.99
WHERE conditionLimits which rows are affectedWHERE product_id = 10

SQL Example

-- Update a single column on a single row
UPDATE customers
SET email = 'updated.email@example.com'
WHERE customer_id = 42;

-- Update multiple columns in one statement
UPDATE customers
SET email = 'updated.email@example.com', phone = '9876543210'
WHERE customer_id = 42;

-- Update multiple rows matching a condition, with a relative update
UPDATE products
SET price = price * 1.10
WHERE category = 'electronics';

-- Safety habit: preview affected rows with SELECT before running UPDATE
SELECT product_id, product_name, price FROM products WHERE category = 'electronics';
-- Only after confirming the above looks correct, run the UPDATE above

The first two examples show targeted, single-row updates affecting one and then two columns. The third updates every electronics product's price relative to its current value, demonstrating both a multi-row update and a relative SET expression. The final SELECT statement illustrates the recommended safety habit of previewing exactly which rows a WHERE condition would affect before committing to the UPDATE.

Real-World Examples

  • Customer profile editing features use single-row UPDATE statements to save changes after a user edits their information.
  • Bulk pricing tools use multi-row UPDATE statements with relative price = price * factor expressions for storewide sales or markups.
  • Order management systems use UPDATE to transition an order's status field as it moves through fulfillment stages.
  • Inventory systems use UPDATE stock_qty = stock_qty - sold_quantity style relative updates whenever a sale is processed.

Best Practices and Pro Tips

  • Always preview an UPDATE's WHERE condition with an equivalent SELECT statement first, especially for multi-row updates affecting unfamiliar or production data.
  • Wrap important UPDATE statements in a transaction (START TRANSACTION ... COMMIT/ROLLBACK) when working in an environment where mistakes need to remain reversible until verified.
  • When updating a column relative to its own value, double-check the direction of the calculation (e.g., * 1.10 for a 10% increase vs * 0.90 for a 10% decrease) — this is an easy place to introduce a sign or logic error.

Common Mistakes to Avoid

  • Running UPDATE without a WHERE clause, accidentally updating every row in the table instead of the intended subset.
  • Forgetting that SET col = col * 1.1 is relative to the current value, not setting col directly to 1.1, leading to confusion about what the statement actually does.
  • Updating the wrong column due to a typo in the SET clause, especially when several similarly-named columns exist on a wide table.
  • Not considering whether other rows or tables (like cached aggregate values or denormalized data) might also need updating to stay consistent after this change.

Interview Questions

Q1. What are the three main parts of an UPDATE statement?

UPDATE table_name specifies the target table, SET column = value specifies which columns change and their new values, and WHERE condition specifies exactly which rows are affected by the update.

Q2. What happens if UPDATE is run without a WHERE clause?

Every single row in the table gets updated according to the SET clause, since there's no condition limiting which rows are affected — this is a common and costly real-world mistake.

Q3. What is a relative update, and how is it written?

A relative update sets a column based on its own current value, such as SET price = price * 1.10;, which increases price by 10% relative to whatever it currently is, rather than setting it to one fixed absolute value for every row.

Q4. What is a recommended safety practice before running a multi-row UPDATE in production?

Run the equivalent SELECT statement using the same WHERE condition first, to preview and confirm exactly which rows would be affected, before converting it into the actual UPDATE statement.

Practice MCQs

1. Which clause in UPDATE specifies which columns change and their new values?

  1. WHERE
  2. FROM
  3. SET
  4. VALUES

Answer: C. SET

Explanation: The SET clause lists the columns being updated along with their new values in an UPDATE statement.

2. What does UPDATE products SET price = price * 0.9; do if it has no WHERE clause?

  1. Decreases price by 10% for every product in the table
  2. Sets price to exactly 0.9 for every product
  3. Fails with an error
  4. Only affects the first product row

Answer: A. Decreases price by 10% for every product in the table

Explanation: Without a WHERE clause, the relative price update applies to every single row in the table, decreasing each product's price by 10% relative to its current value.

3. What is the recommended way to safely preview an UPDATE's effect before running it?

  1. Run the UPDATE and immediately undo it manually
  2. Run an equivalent SELECT with the same WHERE condition first
  3. Skip the WHERE clause entirely
  4. Always run UPDATE twice

Answer: B. Run an equivalent SELECT with the same WHERE condition first

Explanation: Previewing the matching rows with SELECT before running UPDATE confirms exactly which rows will be affected, reducing the risk of an unintended mass update.

Quick Revision Points

  • UPDATE modifies existing rows; SET specifies new column values; WHERE limits which rows are affected.
  • Omitting WHERE updates every row in the table.
  • Relative updates (col = col * factor) compute a new value based on the existing one.
  • Previewing with SELECT using the same WHERE condition is a strong safety habit before UPDATE.

Conclusion

  • UPDATE is how existing data evolves over time without needing to delete and reinsert rows.
  • WHERE is just as critical — and just as risky if omitted — in UPDATE as it is in DELETE.
  • Relative updates are a powerful pattern for percentage- or quantity-based adjustments across many rows at once.

UPDATE modifies existing rows in place, with SET defining the new values and WHERE controlling exactly which rows are targeted — get the WHERE clause wrong, or omit it entirely, and every row in the table can be silently changed. Relative updates, referencing a column's own current value within SET, are a particularly powerful and common real-world pattern. The next lesson covers DELETE, UPDATE's destructive counterpart, with the same level of care around safe, conditional row removal.

Frequently Asked Questions

In standard SQL, no — a single UPDATE statement targets one table. MySQL does support a multi-table UPDATE syntax involving a JOIN, but it requires careful, deliberate syntax and is typically reserved for more advanced use cases.

No, UPDATE only ever modifies existing rows that match the WHERE condition. If no rows match, the statement simply affects zero rows; it never creates new ones — that's what INSERT INTO is for.

Yes, if it's executed within a transaction that hasn't been committed yet, UPDATE can be rolled back like any other DML statement. Once committed (or auto-committed by default outside an explicit transaction), it generally cannot be undone through SQL alone.

Yes, using MySQL's UPDATE ... JOIN syntax, or a subquery within the SET clause, both of which allow a column's new value to be derived from data in another related table.

As many rows as match the WHERE condition — there's no inherent limit imposed by the UPDATE statement itself, which is exactly why an overly broad or missing WHERE clause can affect far more rows than intended.