SQL UPDATE Statement - Modifying Data in SQL Tables
Introduction
Databases are living systems — they evolve. Students change addresses, classes get updated, mistakes are corrected. The UPDATE
statement is your tool to reflect these changes without deleting and reinserting data.
Syntax of UPDATE Statement
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Where:
table_name
: The name of the table you want to updateSET
: Specifies which columns should be updated and their new valuesWHERE
: Filters which rows to update (always use this carefully!)
SQL UPDATE Example – Changing a City
Consider that we have students
table in our database.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Arjun Deshmukh', '10A', 15, 'Pune'),
(2, 'Priya Reddy', '9B', 14, 'Hyderabad'),
(3, 'Karan Mehta', '10A', 15, 'Ahmedabad');
roll_no | name | class | age | city |
---|---|---|---|---|
1 | Arjun Deshmukh | 10A | 15 | Pune |
2 | Priya Reddy | 9B | 14 | Hyderabad |
3 | Karan Mehta | 10A | 15 | Ahmedabad |
Suppose Karan moved from Ahmedabad to Surat. Let’s update his city:
UPDATE students
SET city = 'Surat'
WHERE roll_no = 3;
Table Contents after UPDATE
SELECT * FROM students;
roll_no | name | class | age | city |
---|---|---|---|---|
1 | Arjun Deshmukh | 10A | 15 | Pune |
2 | Priya Reddy | 9B | 14 | Hyderabad |
3 | Karan Mehta | 10A | 15 | Surat |
Updating Multiple Columns
Let’s say Priya changed class and city:
UPDATE students
SET class = '10C', city = 'Bengaluru'
WHERE roll_no = 2;
Table Contents after UPDATE
SELECT * FROM students;
roll_no | name | class | age | city |
---|---|---|---|---|
1 | Arjun Deshmukh | 10A | 15 | Pune |
2 | Priya Reddy | 10C | 14 | Bengaluru |
3 | Karan Mehta | 10A | 15 | Surat |
UPDATE All Rows (Without WHERE)
This is powerful — and dangerous. Without a WHERE
clause, all rows are updated.
UPDATE students
SET age = age + 1;
Table Contents after UPDATE
SELECT * FROM students;
roll_no | name | class | age | city |
---|---|---|---|---|
1 | Arjun Deshmukh | 10A | 16 | Pune |
2 | Priya Reddy | 10C | 15 | Bengaluru |
3 | Karan Mehta | 10A | 16 | Surat |
UPDATE Using Conditions – WHERE with AND
Update students of class 10A who live in Pune:
UPDATE students
SET city = 'Mumbai'
WHERE class = '10A' AND city = 'Pune';
WHERE condition selects all the rows with class 10A and place Pune.
roll_no | name | class | age | city |
---|---|---|---|---|
1 | Arjun Deshmukh | 10A | 16 | Pune |
2 | Priya Reddy | 10C | 15 | Bengaluru |
3 | Karan Mehta | 10A | 16 | Surat |
Table Contents after UPDATE
SELECT * FROM students;
roll_no | name | class | age | city |
---|---|---|---|---|
1 | Arjun Deshmukh | 10A | 16 | Mumbai |
2 | Priya Reddy | 10C | 15 | Bengaluru |
3 | Karan Mehta | 10A | 16 | Surat |
SELECT * FROM students
WHERE class = '10A' AND city = 'Pune';
Using UPDATE with Calculations
Let’s assume we have a student_marks
table:
CREATE TABLE student_marks (
roll_no INT,
subject VARCHAR(30),
marks INT
);
INSERT INTO student_marks VALUES
(1, 'Maths', 80),
(2, 'Maths', 85),
(3, 'Maths', 75);
roll_no | subject | marks |
---|---|---|
1 | Maths | 80 |
2 | Maths | 85 |
3 | Maths | 75 |
Add 5 bonus marks to everyone:
UPDATE student_marks
SET marks = marks + 5;
roll_no | subject | marks |
---|---|---|
1 | Maths | 85 |
2 | Maths | 90 |
3 | Maths | 80 |
Best Practices
- Use WHERE thoughtfully — it's your safety net.
- Preview with SELECT to see what rows will change.
- Back up your data before bulk updates in production.
Summary
The UPDATE
statement lets your database grow and adapt. Whether you're correcting errors, migrating students to a new class, or applying performance bonuses — this is how you make your tables dynamic and relevant.
QUIZ
Question 1:Which SQL statement correctly updates the class of a student named 'Aarav' in the `students` table?
Question 2:If you omit the WHERE clause in an UPDATE statement, all rows in the table will be affected.
Question 3:Which of the following are valid uses of the UPDATE statement?
Question 4:What is the result of this command?
UPDATE students SET name = 'Priya Sharma' WHERE roll_no = 107;
UPDATE students SET name = 'Priya Sharma' WHERE roll_no = 107;
Comments
Loading comments...