Yandex

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;

Parts of the Syntax:

  • table_name: The name of the table you want to update
  • SET: Specifies which columns should be updated and their new values
  • WHERE: Filters which rows to update (always use this carefully!)

1. Sample Table – Students

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');

2. Basic UPDATE – Changing a City

Suppose Karan moved from Ahmedabad to Surat. Let’s update his city:

UPDATE students
SET city = 'Surat'
WHERE roll_no = 3;

3. Viewing the Change

SELECT * FROM students WHERE roll_no = 3;
roll_no | name          | class | age | city
--------+---------------+-------+-----+--------
3       | Karan Mehta   | 10A   | 15  | Surat

4. Updating Multiple Columns

Let’s say Priya changed class and city:

UPDATE students
SET class = '10C', city = 'Bengaluru'
WHERE roll_no = 2;

5. Updating All Rows (Without WHERE)

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.

6. 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';

7. Mistake? Use SELECT Before UPDATE

To avoid accidental changes, always preview affected rows:

SELECT * FROM students
WHERE class = '10A' AND city = 'Pune';

8. 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);

Add 5 bonus marks to everyone:

UPDATE student_marks
SET marks = marks + 5;

9. What Happens If You Skip WHERE?

UPDATE students SET city = 'Delhi';
-- All student cities are now set to Delhi 😨

Lesson: Always double-check your WHERE clause.

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.

What’s Next?

With UPDATE under your belt, it’s time to learn how to remove outdated or incorrect records using the DELETE Statement.

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;

Question 5:UPDATE statements can include expressions, such as increasing marks by 5.

Question 6:Which mistakes can lead to problems when using the UPDATE statement?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You 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.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M