SQL Server · Modifying Data

UPDATE

UPDATE changes existing rows. One missing WHERE clause can change every row in a table — so this lesson focuses on doing it safely.

Beginner 5 min Lesson 11 of 31Examples run on SQL Server 2022 and Azure SQL Database.
Before you start:WHERE
0/31 in SQL Server
On this page · 5 min · Beginner

In simple words#

UPDATE is correcting entries with an eraser. Always say which rows (WHERE) — or you correct every entry in the register.

UPDATE … SET
Change column values.
WHERE
Limits which rows change — never forget it.
@@ROWCOUNT
How many rows were changed.

Learning objectives#

  • Update specific rows with WHERE
  • Set values from expressions
  • Update using data from another table
  • Review changes with OUTPUT and transactions

Explanation#

UPDATE Table SET Column = value WHERE condition changes matching rows. Without WHERE, every row is updated.

SET can use expressions such as Price = Price * 1.10.

OUTPUT deleted.Column shows the old value and inserted.Column the new value — perfect for checking what changed.

Run risky updates inside BEGIN TRAN so you can ROLLBACK if the row count is not what you expected.

Syntax#

SQL Illustrative
UPDATE Table
SET col = expression
OUTPUT deleted.col, inserted.col
WHERE condition;

Code example: A 10% price rise on stationery#

SQLA 10% price rise on stationery Illustrative
UPDATE Products
SET Price = Price * 1.10
OUTPUT deleted.Name, deleted.Price AS OldPrice, inserted.Price AS NewPrice
WHERE Category = 'Stationery';
Expected output
Name      OldPrice  NewPrice
Pen       20.00     22.00
Notebook  60.00     66.00

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. WHERE limits the change to the two Stationery products.
  2. Price * 1.10 is calculated for each row.
  3. deleted holds the row before the change; inserted holds it after.

Real-world use case#

Applying a seasonal price change or marking orders as shipped are typical UPDATE operations in back-office tools.

Common mistakes#

Forgetting WHERE

UPDATE Products SET Price = 0 sets every price to zero. Write the WHERE first, or test with a SELECT using the same condition.

Updating from a join with duplicates

If the joined table has several matching rows, the value applied is unpredictable. Make the join unique.

Best practices#

  • Write and test the WHERE as a SELECT first
  • Use BEGIN TRAN for manual updates and check @@ROWCOUNT
  • Use OUTPUT to audit changes
  • Update in batches for very large tables

Remember this#

UPDATE without WHERE changes every row.

Run the SELECT with the same WHERE first.

Use a transaction for important updates.

Practice exercise#

Your task: Mark order 103 as Delivered and show its old and new status.

Show solution
SQL Illustrative
UPDATE Orders SET Status = 'Delivered' OUTPUT deleted.Status AS OldStatus, inserted.Status AS NewStatus WHERE OrderId = 103;

Mini quiz#

UPDATE quizTrue or false Question 1 of 2

An UPDATE without WHERE changes only the first row.

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.