What is UPDATE?
UPDATE changes existing data in a table. Use it when you need to modify values that are already stored.
The Syntax
UPDATE table_name
SET column = new_value
WHERE condition;Example: Change a Grade
Before: (see table below)
students table - Before UPDATE
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
3 rows
UPDATE students
SET grade = 'A+'
WHERE id = 1;After: Alice's grade changes from 'A' to 'A+'
Update Multiple Columns
UPDATE students
SET grade = 'A', age = 21
WHERE id = 2;Changes both grade AND age for Bob.
How UPDATE Works (Step by Step)
Step 1: Write UPDATE and table name
Step 2: Write SET with column = new value
Step 3: Write WHERE to specify which rows
Step 4: Add semicolon ;
UPDATE students -- Step 1
SET grade = 'A+' -- Step 2
WHERE id = 1; -- Step 3 & 4WARNING: Always Use WHERE!
Without WHERE, ALL rows get updated:
UPDATE students SET grade = 'F';
-- DANGER: Everyone gets F!Always add WHERE:
UPDATE students SET grade = 'F' WHERE id = 1;
-- Safe: Only id=1 gets FQuick Reference
UPDATE table SET col = val WHERE condition- Update specific rowsSET col1 = val1, col2 = val2- Update multiple columns- Always use WHERE to avoid updating all rows!
Tip: Test your WHERE clause with SELECT first to see which rows will be affected!
Try UPDATE
Change the WHERE condition or SET values and see the table update
Current Table: students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
3 rows
SQL Editor
Loading...