Lesson 27 of 60 – UPDATE Statement
45%

UPDATE Statement in SQL

The UPDATE statement is used to modify existing records in a database table. It can be used to change one or more column values for one or more rows.

Note: Always use a proper WHERE condition when updating specific records. Without WHERE, all matching rows in the table can be updated.

1. What is UPDATE?

The UPDATE statement is used to change existing data inside a table.

UPDATE Students
SET city = 'Patna'
WHERE student_id = 101;

This changes the city of the student whose ID is 101 to Patna.

2. Basic UPDATE Syntax

UPDATE table_name
SET column_name = value
WHERE condition;

The SET clause specifies the new value, while WHERE identifies the records that should be updated.

3. UPDATE a Single Column

UPDATE Students
SET city = 'Delhi'
WHERE student_id = 105;

Only the city of student 105 is changed.

4. UPDATE Multiple Columns

You can update multiple columns in the same UPDATE statement.

UPDATE Students
SET city = 'Delhi',
    course = 'Python'
WHERE student_id = 105;

Both city and course are changed for student 105.

5. UPDATE with WHERE

WHERE is used to select the records that should be modified.

UPDATE Students
SET status = 'Active'
WHERE student_id = 101;

Only the student with ID 101 is updated.

6. UPDATE Without WHERE

If WHERE is omitted, the UPDATE statement can modify every row in the table.

UPDATE Students
SET status = 'Active';
Warning: This statement updates the status of every student. Always check your WHERE condition before running an UPDATE.

7. UPDATE Text Values

Text values are normally written inside quotes.

UPDATE Students
SET course = 'Java'
WHERE student_id = 110;

The course of student 110 is changed to Java.

8. UPDATE Numeric Values

Numeric values can be updated directly without quotes.

UPDATE Students
SET fee = 15000
WHERE student_id = 101;

The fee of student 101 is changed to 15000.

9. UPDATE Using Multiple Conditions

UPDATE Students
SET status = 'Active'
WHERE city = 'Patna'
AND course = 'Python';

This updates students who are from Patna and are studying Python.

10. UPDATE with OR

UPDATE Students
SET status = 'Active'
WHERE city = 'Patna'
OR city = 'Delhi';

This updates students from either Patna or Delhi.

11. UPDATE with IN

The IN operator can be used with UPDATE to modify records matching multiple values.

UPDATE Students
SET status = 'Active'
WHERE city IN ('Patna', 'Delhi', 'Gaya');

This updates students from the specified cities.

12. UPDATE with NOT IN

UPDATE Students
SET status = 'Inactive'
WHERE city NOT IN ('Patna', 'Delhi');

This updates students whose city is not Patna or Delhi.

13. UPDATE with BETWEEN

UPDATE Students
SET category = 'Adult'
WHERE age BETWEEN 18 AND 30;

This updates students whose age is between 18 and 30.

14. UPDATE with LIKE

UPDATE Students
SET status = 'Verified'
WHERE name LIKE 'A%';

This updates students whose names start with A.

15. UPDATE NULL Values

You can set a column to NULL if the column allows NULL values.

UPDATE Students
SET email = NULL
WHERE student_id = 101;

This removes the stored email value by setting it to NULL.

16. UPDATE NULL Records

IS NULL can be used in the WHERE condition.

UPDATE Students
SET status = 'Pending'
WHERE email IS NULL;

This updates students whose email is NULL.

17. UPDATE Non-NULL Records

UPDATE Students
SET status = 'Verified'
WHERE email IS NOT NULL;

This updates students who have an email value.

18. UPDATE a Date

Date columns can also be updated.

UPDATE Students
SET admission_date = '2026-09-20'
WHERE student_id = 101;

This changes the admission date for student 101.

19. UPDATE a Boolean or Status Value

UPDATE Students
SET active = 1
WHERE student_id = 101;

If the active column is designed to use 1 for active and 0 for inactive, this marks the student as active.

20. UPDATE Using an Expression

You can update a numeric value using an expression based on its existing value.

UPDATE Students
SET fee = fee + 1000
WHERE course = 'Python';

This increases the fee by 1000 for Python students.

21. Decrease a Value Using UPDATE

UPDATE Students
SET fee = fee - 500
WHERE course = 'Java';

This decreases the fee by 500 for Java students.

22. UPDATE Multiple Records

One UPDATE statement can modify multiple rows when several rows satisfy the WHERE condition.

UPDATE Students
SET status = 'Active'
WHERE course = 'Python';

All matching Python student records are updated.

23. UPDATE with Multiple Columns and Conditions

UPDATE Students
SET course = 'Python',
    fee = 15000,
    status = 'Active'
WHERE city = 'Patna'
AND age BETWEEN 18 AND 30;

This updates multiple columns for students from Patna whose age is between 18 and 30.

24. UPDATE Using a Subquery

An UPDATE statement can use a subquery when the new value or condition depends on another query.

UPDATE Students
SET fee = (
    SELECT MAX(fee)
    FROM Students
)
WHERE student_id = 101;

This sets the fee of student 101 to the maximum fee found in the Students table.

25. UPDATE with CASE

CASE can be used to assign different values based on conditions.

UPDATE Students
SET category =
CASE
    WHEN age < 18 THEN 'Minor'
    WHEN age >= 18 THEN 'Adult'
    ELSE 'Unknown'
END;

This assigns a category based on the student's age.

26. Checking Data Before UPDATE

Before running an UPDATE, it is a good practice to run a SELECT using the same WHERE condition.

SELECT *
FROM Students
WHERE city = 'Patna'
AND course = 'Python';

After checking the returned records, you can apply the UPDATE.

UPDATE Students
SET status = 'Active'
WHERE city = 'Patna'
AND course = 'Python';

27. Common UPDATE Mistake

A very common mistake is forgetting the WHERE clause.

Dangerous:

UPDATE Students
SET fee = 10000;

This can change the fee of every row.

Safer:

UPDATE Students
SET fee = 10000
WHERE student_id = 101;

This changes only the specified student.

28. UPDATE and Transactions

In database systems that support transactions, UPDATE operations can be used inside a transaction so that changes can be reviewed and either committed or rolled back.

START TRANSACTION;

UPDATE Students
SET fee = 15000
WHERE student_id = 101;

COMMIT;

The exact transaction behavior depends on the database engine and storage configuration.

29. Practical Example

Suppose an institute wants to increase the fee of all Python students from 10000 to 12000.

UPDATE Students
SET fee = 12000
WHERE course = 'Python'
AND fee = 10000;

This changes only Python students whose current fee is exactly 10000.

30. Complete UPDATE Example

UPDATE Students
SET fee = 15000,
    status = 'Active',
    city = 'Patna'
WHERE course IN ('Python', 'Java')
AND age BETWEEN 18 AND 30
AND email IS NOT NULL;

This query:

  • Updates students studying Python or Java.
  • Checks that their age is between 18 and 30.
  • Requires an available email value.
  • Changes the fee to 15000.
  • Changes the status to Active.
  • Changes the city to Patna.

📌 Key Points

  • UPDATE is used to modify existing records.
  • The SET clause specifies the new values.
  • The WHERE clause identifies which records should be updated.
  • Without WHERE, an UPDATE can affect every row in the table.
  • Multiple columns can be updated in one statement.
  • UPDATE can be combined with AND, OR, IN, BETWEEN, LIKE, and NULL conditions.
  • Existing numeric values can be increased or decreased using expressions.
  • Always check the WHERE condition before running an UPDATE.
  • A SELECT query using the same WHERE condition can help verify the target rows.
  • Transactions can be used when supported to commit or roll back changes.
  • UPDATE should be used carefully because it changes existing data.

🧠 Quick Quiz

Question: Which SQL statement is used to modify existing records in a table?