SQL Tutorial › Modifying Data › UPDATE

SQL UPDATE Statement

BeginnerLesson 19 of 6911 min read7 queries run on MySQL 8.4

UPDATE changes values in rows that already exist: give a raise, fix a typo, mark an order as shipped. It is simple to write and easy to get dangerously wrong, because without a WHERE clause it changes every row. This lesson shows the safe way to do it, step by step.

Advertisement
In this lesson
  • Update one or several columns with SET
  • Target the right rows with WHERE
  • Use calculations, CASE and joins in an UPDATE
  • Check your change before and after

UPDATE syntax

MySQL
UPDATE table_nameSET column1 = value1, column2 = value2, ...WHERE condition;

Sample data

employees · 12 rows (some columns)
emp_idfirst_namedept_idjob_titlesalarycity
100MeeraNULLCEO250000Mumbai
101Asha1Engineering Manager125000Bengaluru
102Rahul1Senior Developer98000Bengaluru
103Priya1QA Engineer72000Pune
104Karan1Developer72000Bengaluru
105Vikram2Sales Manager105000Mumbai
106Neha2Sales Executive54000Mumbai
107Arjun2Sales Executive48000Kolkata
108Kavya3Marketing Lead88000Pune
109Rohan3Content Writer45000Pune
110Sneha4HR Manager78000Delhi
111Amit1Intern25000Bengaluru

Update one row

Karan has been promoted:

SQL
UPDATE employeesSET job_title = 'Senior Developer'WHERE emp_id = 104; SELECT emp_id, first_name, job_title FROM employees WHERE emp_id = 104;
ResultMySQL 8.4
mysql> UPDATE employees
Query OK, 1 row affected
mysql> SELECT emp_id, first_name, job_title FROM employees WHERE emp_id = 104
emp_idfirst_namejob_title
104KaranSenior Developer
1 row in set
How this query works, step by step
  1. WHERE emp_id = 104 selects exactly one row, because emp_id is the primary key.
  2. SET job_title = 'Senior Developer' replaces the old value in that row.
  3. MySQL reports 1 row affected. The SELECT confirms the change.

Update several columns

Separate the assignments with commas:

SQL
UPDATE employeesSET city = 'Hyderabad', salary = 80000WHERE emp_id = 103; SELECT emp_id, first_name, city, salary FROM employees WHERE emp_id = 103;
ResultMySQL 8.4
mysql> UPDATE employees
Query OK, 1 row affected
mysql> SELECT emp_id, first_name, city, salary FROM employees WHERE emp_id = …
emp_idfirst_namecitysalary
103PriyaHyderabad80000
1 row in set
Advertisement

Calculated updates

The new value can use the old one. A 10% raise for the Marketing department:

MySQL
UPDATE employeesSET salary = salary * 1.10WHERE dept_id = 3; SELECT first_name, salary FROM employees WHERE dept_id = 3;
ResultMySQL 8.4
mysql> UPDATE employees
Query OK, 2 rows affected
mysql> SELECT first_name, salary FROM employees WHERE dept_id = 3
first_namesalary
Kavya96800
Rohan49500
2 rows in set
How this query works, step by step
  1. WHERE dept_id = 3 picks the two Marketing employees.
  2. For each, MySQL reads the current salary, multiplies it by 1.10 and stores the result.
  3. salary is an INT column, so MySQL rounds the result to a whole number: 88,000 becomes 96,800, and 45,000 becomes 49,500.

Update with CASE

Different raises for different job levels, in one statement:

MySQL
UPDATE employeesSET salary = CASE               WHEN salary < 50000 THEN salary + 5000               WHEN salary < 100000 THEN salary + 3000               ELSE salary             ENDWHERE dept_id = 2; SELECT first_name, salary FROM employees WHERE dept_id = 2;
ResultMySQL 8.4
mysql> UPDATE employees
Query OK, 2 rows affected
mysql> SELECT first_name, salary FROM employees WHERE dept_id = 2
first_namesalary
Vikram105000
Neha57000
Arjun53000
3 rows in set
How this query works, step by step
  1. WHERE dept_id = 2 picks the three Sales employees.
  2. The CASE works out each person’s new salary: Arjun (48,000) gets 5,000 more, Neha (54,000) gets 3,000 more.
  3. Vikram (105,000) falls into ELSE salary, so his value stays the same.
  4. MySQL reports 2 rows affected although 3 rows matched: it counts only rows whose value actually changed.
Advertisement

UPDATE with JOIN

Update one table using values from another. Move everyone in the Sales department to the department’s own location:

MySQL
UPDATE employees eJOIN departments d ON d.dept_id = e.dept_idSET e.city = d.locationWHERE d.dept_name = 'Sales'; SELECT e.first_name, e.city, d.locationFROM employees e JOIN departments d ON d.dept_id = e.dept_idWHERE d.dept_name = 'Sales';
ResultMySQL 8.4
mysql> UPDATE employees e
Query OK, 1 row affected
mysql> SELECT e.first_name, e.city, d.location
first_namecitylocation
VikramMumbaiMumbai
NehaMumbaiMumbai
ArjunMumbaiMumbai
3 rows in set
How this query works, step by step
  1. The join pairs each employee with their department row.
  2. WHERE d.dept_name = 'Sales' keeps the Sales employees.
  3. SET e.city = d.location copies Mumbai into each one’s city. Arjun moves from Kolkata; Vikram and Neha were already in Mumbai, so only 1 row changes.
Note

UPDATE ... JOIN is MySQL syntax. PostgreSQL uses UPDATE employees SET ... FROM departments WHERE ..., and SQL Server uses UPDATE e SET ... FROM employees e JOIN ....

Update with a subquery

The WHERE clause can use a subquery. Put every product that has never been ordered on sale, 300 off:

SQL
UPDATE productsSET price = price - 300WHERE product_id NOT IN (SELECT product_id FROM order_items); SELECT product_id, product_name, price FROM products WHERE product_id = 9;
ResultMySQL 8.4
mysql> UPDATE products
Query OK, 1 row affected
mysql> SELECT product_id, product_name, price FROM products WHERE product_id …
product_idproduct_nameprice
9Monitor Arm2499
1 row in set

Safe UPDATE habits

  1. Run the WHERE as a SELECT first. SELECT * FROM employees WHERE dept_id = 3; shows exactly which rows will change.
  2. Use a transaction for important changes, so you can ROLLBACK a mistake (Transactions).
  3. Turn on safe updates in interactive sessions: SET SQL_SAFE_UPDATES = 1; makes MySQL refuse UPDATE and DELETE without a key-based WHERE or LIMIT. MySQL Workbench enables it by default.
MySQL
SET SQL_SAFE_UPDATES = 1;UPDATE employees SET salary = 0;
ResultMySQL 8.4
mysql> SET SQL_SAFE_UPDATES = 1
Query OK, 0 rows affected
mysql> UPDATE employees SET salary = 0
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. 
Advertisement

Exercise: Restock

Set the stock of the USB-C Hub (product 3) to 75 and show the product.

Expected resultMySQL 8.4
mysql> UPDATE products
Query OK, 1 row affected
mysql> SELECT product_id, product_name, stock FROM products WHERE product_id …
product_idproduct_namestock
3USB-C Hub75
1 row in set
Hint 1

UPDATE products SET stock = 75 WHERE product_id = 3;

Exercise: Price rise

Raise the price of every Stationery product by 20 and show the Stationery products afterwards.

Expected resultMySQL 8.4
mysql> UPDATE products
Query OK, 2 rows affected
mysql> SELECT product_name, price FROM products WHERE category = 'Stationery'
product_nameprice
Notebook Pack269
Gel Pens219
2 rows in set
Hint 1

SET price = price + 20 with a WHERE on category.

Quick quiz

1. What does UPDATE products SET stock = 0; do?

2. How do you update two columns in one statement?

3. MySQL says “Rows matched: 3 Changed: 2”. Why only 2?

4. What is the best first step before a risky UPDATE?

Advertisement

UPDATE FAQ

How do I update multiple columns in SQL?

List them after SET, separated by commas: UPDATE t SET a = 1, b = 'x' WHERE id = 5;

How do I update a table using values from another table?

In MySQL, join the tables in the UPDATE: UPDATE t1 JOIN t2 ON ... SET t1.col = t2.col WHERE ...;

Can I undo an UPDATE?

Only if it ran inside a transaction that has not been committed: run ROLLBACK. With autocommit on (the default), the change is permanent, so restore from a backup.

What is safe update mode in MySQL?

SQL_SAFE_UPDATES = 1 blocks UPDATE and DELETE statements that have no WHERE on a key column and no LIMIT, protecting you from changing every row by accident.