Recipe · SQL recipes

UPDATE with CASE: updating several rows with different values

Use CASE inside SET to give several rows different values. Limit the scope with WHERE and keep the previous value with ELSE where appropriate.

Before continuing: This entry is part of the UPDATE guide. If you are new to the topic, start with UPDATE.

Query

In the teaching database, Mouse has 18 units, Keyboard 7 and Monitor 24 has 4. Suppose a physical stock count confirms 20, 9 and 6 units respectively. First review the target rows; then you can run the change inside a transaction and check the result before deciding whether to commit it.

SQLite · run and rolled back
BEGIN;

SELECT id, name, stock
FROM products
WHERE id IN (1, 2, 3)
ORDER BY id;

UPDATE products
SET stock = CASE id
  WHEN 1 THEN 20
  WHEN 2 THEN 9
  WHEN 3 THEN 6
  ELSE stock
END
WHERE id IN (1, 2, 3);

SELECT id, name, stock
FROM products
WHERE id IN (1, 2, 3)
ORDER BY id;

ROLLBACK;

The UPDATE affects exactly three rows. Because the example ends with ROLLBACK, the database goes back to 18, 7 and 4 units. The transaction lets you review the resulting state, but the main safeguard is still a condition that identifies the right rows.

How it works

The simple form CASE key WHEN ... works well when you compare the same column with several specific values. If each branch needs a different condition, you can use CASE WHEN condition THEN value ... END.

ELSE column_name keeps the current value when no branch matches, but it does not replace WHERE. Keeping both makes the intent clear: the WHERE limits the scope and the ELSE stops an unexpected row from becoming NULL if the expression changes later.

Cases that change the result

Simple CASE or searched CASE

When each row is identified by a key

Use the simple form when every branch compares the same expression:

CASE simple
SET stock = CASE id
  WHEN 1 THEN 20
  WHEN 2 THEN 9
  WHEN 3 THEN 6
  ELSE stock
END

When each branch has a different condition

The searched form allows more expressive conditions. For example, you could apply different calculations by stock level:

Searched CASE
UPDATE products
SET stock = CASE
  WHEN stock = 0 THEN 5
  WHEN stock < 5 THEN stock + 3
  ELSE stock
END
WHERE stock < 5;

In both cases, CASE returns a result expression. It is not a loop and does not run several UPDATEstatements; the engine evaluates the expression for each row within the statement's scope.

Three checks that prevent mass updates

1. Test the same WHERE with SELECT first

Before changing anything, run a SELECT with the same condition. If you expect three rows and thirty appear, the problem is the scope, and you can still fix it in time.

2. Do not rely on ELSE alone

A ELSE stock can leave rows that match no branch unchanged, but an UPDATE without WHERE still evaluates every row in the table. That adds work, makes the logic harder to follow and can affect other columns as the statement evolves.

3. Avoid CASE without ELSE on a NOT NULL column

If no branch matches and there is no ELSE, CASE returns NULL. On a column that does not accept nulls, that can cause an error; on one that does, it can store a value you never intended.

When a huge CASE is a bad idea

A CASE with three, five or ten keys can be easy to audit. Hundreds of branches bury the relationship between key and value inside a statement that is hard to review. When the new values come from another data set, it is usually better to represent them as rows and relate them to the target table.

Depending on the engine, that can be done with UPDATE ... FROM, UPDATE ... JOIN, a temporary table, a CTE or an equivalent approach. The page UPDATE with JOIN / UPDATE FROM explains those differences. There is no universal performance rule: the choice depends on the batch size, indexes, plan and engine.