CASE and UPDATE Statements

How do I write an update statement in SQL?

Learn the basic SQL UPDATE statement with examples: use SET to change column values and WHERE to pick rows, and preview changes before you run one.

On this page
  1. What each part does
  2. Update more than one column
  3. Use the current value
  4. What happens without WHERE
  5. Check before you run it
  6. UPDATE and INSERT
  7. Different values for different rows

An UPDATE statement changes rows that already exist in a table. The SET clause names the columns and their new values, and the WHERE clause chooses which rows change:

UPDATE table_name
SET column_name = new_value
WHERE condition;

The examples use a dbo.Students table with StudentID, StudentName, Score, Grade and Status columns. For example, this changes the grade of the student with StudentID 2 to B:

UPDATE dbo.Students
SET Grade = 'B'
WHERE StudentID = 2;

What each part does

  • UPDATE names the table to change.
  • SET gives one or more column = value pairs.
  • WHERE limits the update to matching rows. It is optional, but leaving it out has a large effect, as explained below.

Update more than one column

Separate the assignments with commas:

UPDATE dbo.Students
SET Grade = 'A',
    Status = 'Passed'
WHERE StudentID = 1;

Use the current value

The value on the right of = can use the column’s current value. This adds 5 to the score of the student with StudentID 3:

UPDATE dbo.Students
SET Score = Score + 5
WHERE StudentID = 3;

What happens without WHERE

If you leave out WHERE, the statement changes every row in the table:

UPDATE dbo.Students
SET Status = 'Reviewed';

Every student now has Reviewed in Status. A missing WHERE is a common mistake, so it is worth checking the filter before you run any update.

Check before you run it

  • Preview the rows. Run a SELECT with the same WHERE clause first to see which rows match:
SELECT StudentID, StudentName, Score
FROM dbo.Students
WHERE StudentID = 3;
  • Use a transaction. In SQL Server, put the update between BEGIN TRANSACTION and a COMMIT TRANSACTION or ROLLBACK TRANSACTION. Check the rows, then commit to keep the change or roll back to undo it.
  • Keep a backup before a large change to important data.

UPDATE and INSERT

UPDATE changes rows that are already in the table. INSERT adds new rows. If you need to add a student rather than change one, what is an insert statement explains that form.

Different values for different rows

Sometimes each row needs a value that depends on its own data. In that case, put a CASE expression in the SET clause. The pattern is explained in the guide to a case statement in update query in sql server.

The basic form of UPDATE works in most SQL databases. SQL Server and other systems add their own options, so check the documentation for the system you use.

The complete guide to case and update statements

Using a CASE Statement in an UPDATE Query in SQL Server
Read the guide

Keep exploring

Guides from other topics

All topics →