Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 144

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.

💡 One UPDATE + a WHERE changes all matching rows at once. Use CASE for row-specific values.

Update by condition

UPDATE students
SET marks = marks + 5
WHERE class = 10;
Query OK, 25 rows affected (0.02 sec)

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 + 5 to 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?
One UPDATE with a WHERE clause changes every matching row at once: 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?
Use a CASE expression in the SET clause: 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?
Use an UPDATE with a JOIN: 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?
Yes. You can reference the column on the right side: SET price = price * 1.1 raises every matching price by 10%. MySQL uses the existing value to compute the new one for each row.
How do I update multiple rows safely?
Always include a WHERE clause, and test it first with a SELECT using the same condition to preview which rows will change. For risky updates, wrap them in a transaction so you can ROLLBACK if the result is wrong.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicJoin Three or More Tables Your progress: 144 / 151 (95%) · Lesson 145 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free