MySQL UPDATE Statement

MySQL Tutorial

MySQL UPDATE Statement

The UPDATE statement in MySQL is used to modify existing records in a table.

Be careful: Without a WHERE clause, all rows will be updated.


 What is UPDATE?

  • Modifies existing data

  • Can update one or multiple columns

  • Works with WHERE, ORDER BY, LIMIT, and JOIN


 Basic Syntax

 Simple Example

Update a Single Column

  •  Updates marks of only one student

 Updates Multiple Columns

 UPDATE Without WHERE (Dangerous)

  •  Updates all rows in the table

 Always check with SELECT first:


UPDATES with WHERE Conditions

The WHERE clause is used to update specific rows only.
Without WHERE, all rows in the table will be updated.

  •  Increases salary for HR department only

 UPDATES with LIMIT (MySQL Feature)

LIMIT restricts how many rows will be updated.
This is MySQL-specific and not supported in all databases.

  •  Updates lowest 3 marks

 UPDATES with ORDER BY

ORDER BY decides which rows should be updated first.
Mostly used together with LIMIT.

  • Updates latest 5 employees

UPDATE with JOIN (Very Important)

Updates one table using data from another table.

  •  Common in real projects & interviews

UPDATE Subquery

UPDATE-with CASE

  •  Conditional updates in a single query

UPDATE vs INSERT vs DELETE (Interview)

CommandPurpose
INSERTAdd new rows
UPDATEModify existing rows
DELETERemove rows

Common Mistakes

  •  Forgetting WHERE clause
  •  Wrong condition (updates wrong rows)
  •  Not checking affected rows
  •  Updating primary/foreign keys blindly

 Check affected rows:


Interview Questions & MCQs (Very Important)

Q1. What does UPDATE do?

A) Insert rows
B) Modify rows
C) Delete rows
D) Select rows

Answer: B


Q2. What happens if WHERE clause is omitted?

A) Error
B) Only one row updated
C) All rows updated
D) No rows updated

Answer: C


Q3. Can UPDATE modify multiple columns?

A) Yes
B) No

Answer: A


Q4. Can UPDATE be used with JOIN in MySQL?

A) Yes
B) No

Answer: A


Q5. Which clause limits number of updated rows?

A) GROUP BY
B) HAVING
C) LIMIT
D) OFFSET

Answer: C


Q6. Which is safest practice before UPDATE?

A) DROP table
B) SELECT with same WHERE
C) COMMIT
D) CREATE INDEX

Answer: B


Real-Life Use Cases

  •  Salary revision
  • Status updates
  • Data correction
  •  Bulk changes
  • Conditional updates

 Summary

  • UPDATE modifies existing data

  • Use WHERE to avoid full-table updates

  • Supports JOIN, CASE, LIMIT

  • Very powerful but risky if misused

  • Crucial for SQL exams & interviews

You may also like...