UPDATE Statement
Learn how to modify existing rows in SQL Server using UPDATE, including updating multiple columns and the danger of an UPDATE with no WHERE clause.
Introduction
Data changes over time — an employee gets a raise, a customer changes their address, a product's price is adjusted. The UPDATE statement is how you modify existing rows in a SQL Server table. It is one of the most powerful, and most dangerous, statements in T-SQL, because a small mistake can silently modify far more data than you intended.
- The basic syntax of the UPDATE statement.
- How to update multiple columns in a single statement.
- Why an UPDATE without a WHERE clause is extremely dangerous.
- How to verify your changes safely before committing them.
Basic UPDATE Syntax
An UPDATE statement specifies the table to modify, a SET clause listing the column(s) to change and their new values, and typically a WHERE clause identifying which rows to affect.
UPDATE EmployeesSET Salary = 70000.00WHERE EmployeeID = 3;
SELECT EmployeeID, FirstName, SalaryFROM EmployeesWHERE EmployeeID = 3;Click Run to see what this code prints.
Updating Multiple Columns
You can update several columns in one statement by separating each column/value pair with a comma in the SET clause. All the changes are applied together as a single, atomic update to each matching row.
UPDATE EmployeesSET Department = 'Senior Engineering', Salary = Salary * 1.10WHERE EmployeeID = 3;
SELECT EmployeeID, FirstName, Department, SalaryFROM EmployeesWHERE EmployeeID = 3;Click Run to see what this code prints.
Notice that Salary = Salary * 1.10 references the column's current value on the right-hand side to calculate a 10% raise — SQL Server evaluates the expression using the row's existing values before applying the update.
The Danger of UPDATE Without WHERE
If you omit the WHERE clause, SQL Server updates every single row in the table — not just the one you intended. This is one of the most common and costly mistakes in SQL, and it has caused real production incidents. Always double-check your WHERE clause before running an UPDATE.
-- WARNING: this updates EVERY row in the table!UPDATE EmployeesSET Salary = 70000.00;Without a WHERE clause, every row in Employees would have its Salary overwritten to 70000.00, including rows that were never meant to change. Always include a WHERE clause unless you deliberately intend to update the entire table.
Verifying Before You UPDATE
A reliable safety habit is to first run a SELECT with the exact same WHERE clause you plan to use in the UPDATE. This lets you confirm precisely which rows will be affected before you actually change any data.
-- Step 1: Check which rows will be affectedSELECT * FROM Employees WHERE Department = 'Sales';
-- Step 2: Once confirmed, run the UPDATE with the same WHERE clauseUPDATE EmployeesSET Department = 'Regional Sales'WHERE Department = 'Sales';Click Run to see what this code prints.
Common Mistakes
- Running UPDATE without a WHERE clause, which modifies every row in the table.
- Forgetting to run the UPDATE inside a transaction when working on production data, making an accidental mistake harder to undo.
- Assuming an UPDATE inside a stored procedure or script has been tested — always verify with SELECT first.
- Updating a column that is used as a JOIN or foreign key without considering the downstream impact on related tables.
Best Practices
- Always include a WHERE clause unless you truly intend to update every row.
- Run a SELECT with the same WHERE clause first to preview affected rows.
- Wrap critical updates in a transaction (BEGIN TRANSACTION / COMMIT / ROLLBACK) so you can undo mistakes before committing.
- Use TOP with an UPDATE cautiously when batching large updates to avoid locking too many rows at once.
Frequently Asked Questions
Only if it was run inside an uncommitted transaction, using ROLLBACK. Once committed, you would need a database backup or transaction log backup to restore the previous data.
Yes. You can set a column to the result of a subquery, for example SET Salary = (SELECT AVG(Salary) FROM Employees), as long as the subquery returns a single value per row.
Yes. SSMS displays a message like "(3 rows affected)" after an UPDATE runs, which is a useful sanity check against how many rows you expected to change.
Key Takeaways
- UPDATE modifies existing rows using a SET clause.
- Multiple columns can be updated in a single statement.
- Omitting WHERE updates every row in the table — always double check.
- Previewing affected rows with SELECT first is a critical safety habit.
- Wrapping updates in a transaction allows you to roll back mistakes.
Summary
UPDATE is essential for keeping data current, but it demands care, especially around the WHERE clause. Next, you will learn the equally powerful — and equally risky — DELETE statement for removing rows entirely.