TExcelTAD
Home › SQL › SQL UPDATE

SQL UPDATE

UPDATE changes values in existing rows.

sql
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

The WHERE clause chooses the rows; SET says what to change in them.

Updating one row

Order 9 has just shipped. Set its date and status in one statement:

sql · try it yourself
UPDATE orders
SET shipped_date = '2025-03-26',
    status = 'shipped'
WHERE order_id = 9;

SELECT order_id, order_date, shipped_date, status
FROM orders
WHERE order_id = 9;
1 row affected.

order_id | order_date | shipped_date | status
---------+------------+--------------+--------
9        | 2025-03-24 | 2025-03-26   | shipped
(1 row)

Updating many rows

A WHERE that matches many rows updates all of them. Give a 10% discount on all Snacks:

sql · try it yourself
SELECT name, price FROM products WHERE category_id = 5 ORDER BY name;

UPDATE products
SET price = ROUND(price * 0.9, 2)
WHERE category_id = 5;

SELECT name, price FROM products WHERE category_id = 5 ORDER BY name;
name               | price
-------------------+------
Dark Chocolate Bar | 2.49
Salted Almonds     | 5.99
Tortilla Chips     | 3.49
(3 rows)

3 rows affected.

name               | price
-------------------+------
Dark Chocolate Bar | 2.24
Salted Almonds     | 5.39
Tortilla Chips     | 3.14
(3 rows)

The new value can be computed from the old one: price * 0.9 uses the row's current price.

Restocking

Every out-of-stock product gets 50 new units:

sql · try it yourself
UPDATE products
SET stock = stock + 50
WHERE stock = 0;

SELECT name, stock FROM products WHERE product_id IN (4, 14) ORDER BY name;
2 rows affected.

name            | stock
----------------+------
Avocados        | 50
Sparkling Water | 50
(2 rows)

Forgetting WHERE

Without a WHERE, every row in the table is updated:

sql · try it yourself
UPDATE customers SET city = 'Berlin';

SELECT city, COUNT(*) AS customers FROM customers GROUP BY city;
25 rows affected.

city   | customers
-------+----------
Berlin | 25
(1 row)

All 25 customers now "live" in Berlin. In a real database this is a serious accident — here, just run another example and the data is fresh again.

Tip: Write the WHERE clause first, test it with a SELECT, and only then turn the statement into an UPDATE. Many tools (and MySQL's --safe-updates mode) refuse an UPDATE without WHERE.

Updating with values from another table

A subquery in SET can look up values elsewhere. Raise the price of every product supplied from Japan by 5:

sql · try it yourself
UPDATE products
SET price = price + 5
WHERE supplier_id IN (SELECT supplier_id FROM suppliers WHERE country = 'Japan');

SELECT name, price FROM products WHERE supplier_id = 6 ORDER BY price;
5 rows affected.

name                        | price
----------------------------+------
USB-C Cable                 | 14.99
Wireless Mouse              | 29.99
Power Bank                  | 34.5
Mechanical Keyboard         | 94
Noise-Cancelling Headphones | 154
(5 rows)

SQLite, PostgreSQL and SQL Server also support UPDATE … FROM other_table for joins inside an update; MySQL writes it as UPDATE a JOIN b ON … SET ….

Seeing what changed

Like INSERT, UPDATE supports RETURNING in SQLite and PostgreSQL:

sql · try it yourself
UPDATE employees
SET salary = salary + 2000
WHERE title = 'Sales Rep'
RETURNING first_name, salary;
first_name | salary
-----------+-------
Rosa       | 64000
Tom        | 60000
Aisha      | 62500
Owen       | 57000
(4 rows)

Test yourself

What does UPDATE products SET stock = 0; do?

  1. Nothing, WHERE is missing
  2. Sets stock to 0 for every product
  3. Sets stock for the first product
  4. Raises an error

How do you change two columns in one UPDATE?

  1. Two SET clauses
  2. SET a = 1 AND b = 2
  3. SET a = 1, b = 2
  4. UPDATE twice