SQL UPDATE Statement
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.
- 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
UPDATE table_nameSET column1 = value1, column2 = value2, ...WHERE condition;Sample data
| emp_id | first_name | dept_id | job_title | salary | city |
|---|---|---|---|---|---|
| 100 | Meera | NULL | CEO | 250000 | Mumbai |
| 101 | Asha | 1 | Engineering Manager | 125000 | Bengaluru |
| 102 | Rahul | 1 | Senior Developer | 98000 | Bengaluru |
| 103 | Priya | 1 | QA Engineer | 72000 | Pune |
| 104 | Karan | 1 | Developer | 72000 | Bengaluru |
| 105 | Vikram | 2 | Sales Manager | 105000 | Mumbai |
| 106 | Neha | 2 | Sales Executive | 54000 | Mumbai |
| 107 | Arjun | 2 | Sales Executive | 48000 | Kolkata |
| 108 | Kavya | 3 | Marketing Lead | 88000 | Pune |
| 109 | Rohan | 3 | Content Writer | 45000 | Pune |
| 110 | Sneha | 4 | HR Manager | 78000 | Delhi |
| 111 | Amit | 1 | Intern | 25000 | Bengaluru |
Update one row
Karan has been promoted:
UPDATE employeesSET job_title = 'Senior Developer'WHERE emp_id = 104; SELECT emp_id, first_name, job_title FROM employees WHERE emp_id = 104;| emp_id | first_name | job_title |
|---|---|---|
| 104 | Karan | Senior Developer |
WHERE emp_id = 104selects exactly one row, becauseemp_idis the primary key.SET job_title = 'Senior Developer'replaces the old value in that row.- MySQL reports
1 row affected. TheSELECTconfirms the change.
Update several columns
Separate the assignments with commas:
UPDATE employeesSET city = 'Hyderabad', salary = 80000WHERE emp_id = 103; SELECT emp_id, first_name, city, salary FROM employees WHERE emp_id = 103;| emp_id | first_name | city | salary |
|---|---|---|---|
| 103 | Priya | Hyderabad | 80000 |
Calculated updates
The new value can use the old one. A 10% raise for the Marketing department:
UPDATE employeesSET salary = salary * 1.10WHERE dept_id = 3; SELECT first_name, salary FROM employees WHERE dept_id = 3;| first_name | salary |
|---|---|
| Kavya | 96800 |
| Rohan | 49500 |
WHERE dept_id = 3picks the two Marketing employees.- For each, MySQL reads the current
salary, multiplies it by 1.10 and stores the result. salaryis anINTcolumn, 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:
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;| first_name | salary |
|---|---|
| Vikram | 105000 |
| Neha | 57000 |
| Arjun | 53000 |
WHERE dept_id = 2picks the three Sales employees.- The CASE works out each person’s new salary: Arjun (48,000) gets 5,000 more, Neha (54,000) gets 3,000 more.
- Vikram (105,000) falls into
ELSE salary, so his value stays the same. - MySQL reports 2 rows affected although 3 rows matched: it counts only rows whose value actually changed.
UPDATE with JOIN
Update one table using values from another. Move everyone in the Sales department to the department’s own location:
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';| first_name | city | location |
|---|---|---|
| Vikram | Mumbai | Mumbai |
| Neha | Mumbai | Mumbai |
| Arjun | Mumbai | Mumbai |
- The join pairs each employee with their department row.
WHERE d.dept_name = 'Sales'keeps the Sales employees.SET e.city = d.locationcopies Mumbai into each one’scity. Arjun moves from Kolkata; Vikram and Neha were already in Mumbai, so only 1 row changes.
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:
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;| product_id | product_name | price |
|---|---|---|
| 9 | Monitor Arm | 2499 |
Safe UPDATE habits
- Run the WHERE as a SELECT first.
SELECT * FROM employees WHERE dept_id = 3;shows exactly which rows will change. - Use a transaction for important changes, so you can
ROLLBACKa mistake (Transactions). - Turn on safe updates in interactive sessions:
SET SQL_SAFE_UPDATES = 1;makes MySQL refuseUPDATEandDELETEwithout a key-basedWHEREorLIMIT. MySQL Workbench enables it by default.
SET SQL_SAFE_UPDATES = 1;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.
Exercise: Restock
Set the stock of the USB-C Hub (product 3) to 75 and show the product.
| product_id | product_name | stock |
|---|---|---|
| 3 | USB-C Hub | 75 |
Hint 1
UPDATE products SET stock = 75 WHERE product_id = 3;
UPDATE productsSET stock = 75WHERE product_id = 3; SELECT product_id, product_name, stock FROM products WHERE product_id = 3;| product_id | product_name | stock |
|---|---|---|
| 3 | USB-C Hub | 75 |
Exercise: Price rise
Raise the price of every Stationery product by 20 and show the Stationery products afterwards.
| product_name | price |
|---|---|
| Notebook Pack | 269 |
| Gel Pens | 219 |
Hint 1
SET price = price + 20 with a WHERE on category.
UPDATE productsSET price = price + 20WHERE category = 'Stationery'; SELECT product_name, price FROM products WHERE category = 'Stationery';| product_name | price |
|---|---|
| Notebook Pack | 269 |
| Gel Pens | 219 |
Quick quiz
1. What does UPDATE products SET stock = 0; do?
Without WHERE, every row is updated.
2. How do you update two columns in one statement?
Separate the assignments with commas after one SET.
3. MySQL says “Rows matched: 3 Changed: 2”. Why only 2?
MySQL counts a row as changed only when its value actually differs.
4. What is the best first step before a risky UPDATE?
Seeing the affected rows first prevents surprises.
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.