CSE Roadmap

Core tier · about 3 hours · 5 parts

Part 3 of 5 · 1 min read

Updating based on existing values

Computing a new value from a row's own current value, rather than overwriting with a fixed one.

Think of giving every employee a 10 percent raise, where each new salary depends on that same employee's own current salary, not on one fixed new number applied to everyone.

In plain words: an UPDATE's SET clause can reference the row's own current column values, computing the new value from them. This differs from setting every row to the exact same fixed value, which is what a careless UPDATE often does by accident.

A worked example. Applying a 10 percent discount to every book over 1000, based on each book's own current price.

The key lines.

UPDATE books
SET price = price * 0.90
WHERE price > 1000;

Self-referencing UPDATE

A self-referencing UPDATE computes a new column value from that same row's existing column values, rather than assigning an unrelated fixed value.

Example: SET price = price * 0.90 reduces each matching row's own price by 10 percent, rather than setting every row to one shared number.