Learn SQL: Changing rows with UPDATE

Adjusting prices

Butter has gone up, a fresh batch of eclairs is out of the oven and an order is ready. "Could you just change that?" Jens asks. Nothing to add this time: you change what is already there.

Changing a value

UPDATE changes existing rows. After SET you say which column gets which new value. With WHERE you choose which rows, just like in a SELECT.

Here a croissant costs €1.50 from now on. Below, the changed value lights up.

UPDATE products SET price = 1.5 WHERE name = 'Croissant'
Result

Calculating with the old value

After SET you may calculate with the current value. stock = stock - 3 takes three off, row by row.

To change several columns at once, list them with a comma: SET price = 2, stock = 5.

Here all pastries go up 5%. ROUND(..., 2) rounds to two decimals, so you don't end up with a price like 1.785.

UPDATE products SET price = ROUND(price * 1.05, 2) WHERE category = 'pastry'
Result

Try it yourself

A fresh batch of eclairs is ready. Set the stock of the Eclair to 12.

Ready for more?

The full lesson has 8 exercises, from easy to challenging, with hints, points and a streak you can build.

Start the lesson