The UPDATE statement is used to modify existing records in a table.
It lets you change one or more column values for one or more rows.
Syntax
UPDATE table_name SET column1 = value1, column2 = value2, ...
WHERE condition;
- table_name: The table you want to update.
- SET: Specifies the new values.
- WHERE: Defines which rows should be updated (without it, all rows will be updated!).
Update a Single Column
Changes the age of the student with student_id = 1 to 21.
UPDATE students SET age = 21 WHERE student_id = 1;
Update Multiple Columns
Updates both age and email for the student named Ravi Kumar.
UPDATE students
SET age = 22, email = 'ravi22@example.com'
WHERE name = 'Ravi Kumar';
Update All Rows (No WHERE Clause)
Sets today’s date as admission date for all students.
Warning: Always be careful when skipping WHERE.
UPDATE students SET admission_date = CURRENT_DATE;
Conditional Update
Increases the age by 1 for students admitted before 2024.
UPDATE students
SET age = age + 1
WHERE admission_date < '2024-01-01';