⬅ Previous Topic
INSERT INTO StatementNext Topic ⮕
DELETE Statement in SQL⬅ Previous Topic
INSERT INTO StatementNext Topic ⮕
DELETE Statement in SQLDatabases 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!)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');
Suppose Karan moved from Ahmedabad to Surat. Let’s update his city:
UPDATE students
SET city = 'Surat'
WHERE roll_no = 3;
SELECT * FROM students WHERE roll_no = 3;
roll_no | name | class | age | city
--------+---------------+-------+-----+--------
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;
This is powerful — and dangerous. Without a WHERE
clause, all rows are updated.
UPDATE students
SET age = age + 1;
-- All students are now 1 year older.
Update students of class 10A who live in Pune:
UPDATE students
SET city = 'Mumbai'
WHERE class = '10A' AND city = 'Pune';
To avoid accidental changes, always preview affected rows:
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);
Add 5 bonus marks to everyone:
UPDATE student_marks
SET marks = marks + 5;
UPDATE students SET city = 'Delhi';
-- All student cities are now set to Delhi 😨
Lesson: Always double-check your WHERE clause.
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.
With UPDATE under your belt, it’s time to learn how to remove outdated or incorrect records using the DELETE Statement.
UPDATE students SET name = 'Priya Sharma' WHERE roll_no = 107;
⬅ Previous Topic
INSERT INTO StatementNext Topic ⮕
DELETE Statement in SQLYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.