MySQL UPDATE Statement

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, andJOIN
Basic Syntax
Simple Example
Update a Single Column
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.
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.
UPDATE with JOIN (Very Important)
Updates one table using data from another table.
UPDATE Subquery
UPDATE-with CASE
UPDATE vs INSERT vs DELETE (Interview)
| Command | Purpose |
|---|---|
| INSERT | Add new rows |
| UPDATE | Modify existing rows |
| DELETE | Remove 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
UPDATEmodifies existing dataUse
WHEREto avoid full-table updatesSupports
JOIN,CASE,LIMITVery powerful but risky if misused
Crucial for SQL exams & interviews
