Update Multiple Rows in MySQL
In this lesson: Update many rows at once in MySQL — bulk UPDATE, CASE WHEN and UPDATE JOIN — examples with output. In Hindi & English.
Updating many rows
A single UPDATE can change many rows at once — you do not need one statement per row. With a WHERE clause it targets a group, with expressions it can compute new values, and with CASE it can give each row a different value. Just always update safely.
Update by condition
UPDATE students SET marks = marks + 5 WHERE class = 10;
Every student in class 10 gets 5 bonus marks in one statement. Notice marks = marks + 5 uses each row's current value to compute the new one.
Update several columns
UPDATE employees SET salary = salary * 1.1, status = 'reviewed' WHERE department = 'Sales';
You can set multiple columns in one UPDATE, separated by commas. Here Sales employees get a 10% raise and a status change together.
Different values with CASE
UPDATE students
SET grade = CASE
WHEN marks >= 90 THEN 'A'
WHEN marks >= 75 THEN 'B'
ELSE 'C'
END;
A CASE in the SET clause gives each row a value based on its own data. Here every student's grade is set from their marks — all in one statement.
Update from a join
UPDATE orders o JOIN customers c ON o.customer_id = c.id SET o.city = c.city;
An UPDATE with a JOIN copies values from a related table. Here each order's city is filled from its customer's city, matched by the key — no manual lookups.
Doing it safely
- Always use WHERE (unless you truly mean all rows).
- Test first with a SELECT using the same condition.
- Use a transaction for risky updates, so you can ROLLBACK.
These habits prevent the classic disaster of updating every row by mistake.
Common mistakes
- Omitting WHERE. Then every row is updated.
- Forgetting END in CASE. A CASE must finish with
END. - Not testing with SELECT. Preview the rows before updating.
- Skipping transactions on big updates. They let you undo mistakes.
Summary
- One UPDATE with WHERE changes all matching rows at once.
- Use expressions like
marks + 5to compute from current values. - Set several columns in one statement, separated by commas.
- Use CASE to give each row a different value.
- Update from a JOIN, and always update safely with WHERE and testing.
Next, run these safely with safe UPDATE and DELETE, or add many rows in inserting multiple rows.
Frequently Asked Questions
How do I update multiple rows in MySQL?
UPDATE students SET marks = marks + 5 WHERE class = 10;. All rows where the condition is true are updated in a single statement.How do I update rows to different values in one statement?
SET grade = CASE WHEN marks >= 90 THEN 'A' WHEN marks >= 75 THEN 'B' ELSE 'C' END. This gives each row a value based on its own data, all in one UPDATE.How do I update a table using data from another table?
UPDATE orders o JOIN customers c ON o.customer_id = c.id SET o.city = c.city;. This copies matching values from the joined table into the rows being updated.Can I update a column based on its current value?
SET price = price * 1.1 raises every matching price by 10%. MySQL uses the existing value to compute the new one for each row.