SQL UPDATE Statement
The UPDATE statement is used to modify existing records in a table. When data in the database needs to be corrected or changed, UPDATE is the command to use.
Think of it like editing a form that was already submitted — the record exists, but specific fields need to be changed.
The Reference Table
| StudentID | StudentName | Age | City |
|---|---|---|---|
| 1 | Ravi Sharma | 20 | Delhi |
| 2 | Priya Mehta | 22 | Mumbai |
| 3 | Arjun Nair | 19 | Chennai |
| 4 | Sneha Kapoor | 21 | Pune |
| 5 | Rohit Das | 23 | Kolkata |
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;Important: The WHERE clause specifies which row(s) to update. Without it, all rows in the table will be updated — which is almost never the intended behavior.
Updating a Single Column in One Row
UPDATE Students
SET City = 'Bangalore'
WHERE StudentID = 3;This changes Arjun Nair's city from Chennai to Bangalore.
After Update:
| StudentID | StudentName | Age | City |
|---|---|---|---|
| 1 | Ravi Sharma | 20 | Delhi |
| 2 | Priya Mehta | 22 | Mumbai |
| 3 | Arjun Nair | 19 | Bangalore |
| 4 | Sneha Kapoor | 21 | Pune |
| 5 | Rohit Das | 23 | Kolkata |
Updating Multiple Columns at Once
Multiple columns can be updated in a single statement by separating them with commas in the SET clause.
UPDATE Students
SET Age = 20, City = 'Hyderabad'
WHERE StudentID = 3;This updates both the Age and City for the student with StudentID = 3 in a single operation.
Updating Multiple Rows With a Condition
UPDATE Students
SET City = 'Delhi NCR'
WHERE City = 'Delhi';This updates the City for all students who have 'Delhi' in their City column. If there are multiple students from Delhi, all of them will be updated.
Updating All Rows (Without WHERE)
-- Warning: This updates every row in the table
UPDATE Students
SET Age = 0;Without a WHERE clause, every student's age will be set to 0. This is almost always a mistake, so WHERE should always be included in an UPDATE statement.
Using a Calculation in SET
The SET clause can use the column's own value in the calculation. For example, to increase every student's age by 1:
UPDATE Students
SET Age = Age + 1
WHERE StudentID = 1;This takes Ravi's current age (20) and adds 1 to it, making it 21.
Verifying the Update
After running an UPDATE, use SELECT to confirm the change was applied correctly:
SELECT * FROM Students WHERE StudentID = 3;Key Points to Remember
- Always use the
WHEREclause withUPDATEto target specific rows. - Multiple columns can be updated in one statement — separate them with commas in
SET. - The existing column value can be referenced in the
SETexpression (e.g.,Age = Age + 1). - Run a
SELECTquery first to verify which rows will be affected before running the update.
Summary
The UPDATE statement changes existing data in a table. The SET clause defines what changes to make, and the WHERE clause controls which rows are affected. Using WHERE carefully is the most critical aspect of writing a safe UPDATE query.
