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.
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.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
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!)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;
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 |
Let’s say Priya changed class and city:
UPDATE students
SET class = '10C', city = 'Bengaluru'
WHERE roll_no = 2;
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 |
This is powerful — and dangerous. Without a WHERE clause, all rows are updated.
UPDATE students
SET age = age + 1;
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 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 |
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';
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 |
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.
UPDATE students SET name = 'Priya Sharma' WHERE roll_no = 107;