UPDATE — Change Existing Data
Change values in existing rows with UPDATE … SET … WHERE — one column, many columns, or calculated values.
Dhoni hits a quick 50 and his runs must change from 350 to 400. Warner moves from DC to SRH. Data changes all the time — UPDATE is how you keep it correct.
Definition
UPDATE is a DML statement that modifies the values of one or more columns in existing rows of a table. The SET clause gives the new values, and the WHERE clause decides which rows are changed. Without WHERE, every row in the table is updated.
- Existing rows — UPDATE never adds or removes rows; the row count stays the same.
- SET —
column = new_value, separated by commas for several columns. - WHERE — the filter. Only rows where the condition is TRUE are changed.
Syntax
sql
UPDATE table_name
SET column1 = value1,
column2 = value2
WHERE condition;| Part | Meaning |
|---|---|
UPDATE table_name | Which table to change |
SET column = value | The new values. Separate several columns with commas |
WHERE condition | Which row(s) should be changed |
NOTE: Always use WHERE to avoid updating all rows unintentionally.
Example — change one value
sql
UPDATE players
SET runs = 400
WHERE id = 4;
SELECT * FROM players;After — Dhoni: 350 → 400 runs:
| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 2 | Rohit Sharma | MI | India | 500 |
| 3 | Jos Buttler | RR | England | 850 |
| 4 | MS Dhoni | CSK | India | 400 |
| 5 | David Warner | DC | Australia | 700 |
| 6 | Jasprit Bumrah | MI | India | 20 |
Step by step: 1️⃣ WHERE finds the row: id = 4 → 2️⃣ SET changes runs to 400. Only MS Dhoni's row changes.
Example — change many columns
sql
UPDATE players
SET team = 'SRH',
runs = 760
WHERE id = 5;After — Warner: new team + runs:
| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 2 | Rohit Sharma | MI | India | 500 |
| 3 | Jos Buttler | RR | England | 850 |
| 4 | MS Dhoni | CSK | India | 350 |
| 5 | David Warner | SRH | Australia | 760 |
| 6 | Jasprit Bumrah | MI | India | 20 |
Step by step: 1️⃣ WHERE finds id = 5 → 2️⃣ SET changes 2 columns. Separate columns with commas — both cells on one row change.
Example — calculate from the old value
The new value can use the current value of the column:
sql
UPDATE players
SET runs = runs + 50
WHERE team = 'MI';| id | name | team | runs |
|---|---|---|---|
| 2 | Rohit Sharma | MI | 550 |
| 6 | Jasprit Bumrah | MI | 70 |
Both MI players get +50 (500 → 550, 20 → 70). The WHERE matched 2 rows, so SQL Server says (2 rows affected).
DANGER — UPDATE without WHERE
sql
UPDATE players
SET runs = 0;After — ALL rows changed!
| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 0 |
| 2 | Rohit Sharma | MI | India | 0 |
| 3 | Jos Buttler | RR | England | 0 |
| 4 | MS Dhoni | CSK | India | 0 |
| 5 | David Warner | DC | Australia | 0 |
| 6 | Jasprit Bumrah | MI | India | 0 |
1️⃣ No WHERE = no filter → 2️⃣ EVERY row is changed! Every player's runs became 0. Never forget WHERE!
Update using another table
Use a subquery in WHERE (works in every database):
sql
-- +10 runs for players whose team is based in Mumbai
UPDATE players
SET runs = runs + 10
WHERE team IN (SELECT team FROM teams WHERE city = 'Mumbai');SQL Server also supports UPDATE with a JOIN using FROM:
sql
UPDATE p
SET p.runs = p.runs + 10
FROM players AS p
INNER JOIN teams AS t
ON p.team = t.team
WHERE t.city = 'Mumbai';Common mistakes
- ❌ Forgetting
WHERE→ every row changed. ✅ Write the WHERE first, and check "rows affected". - ❌
SET team = 'SRH' AND runs = 760✅ Use a comma:SET team = 'SRH', runs = 760. (WithAND, SQL Server raises a syntax error; MySQL silently setsteamto a 0/1 value!) - ❌
WHERE name = 'Dhoni'when the stored name is'MS Dhoni'→ 0 rows affected. ✅ Filter by the primary key (id = 4) whenever possible. - ❌
WHERE runs = NULL✅WHERE runs IS NULL—= NULLnever matches. - ❌ Using
UPDATEto add a new player. ✅UPDATEonly changes existing rows; useINSERT.
Interview tip
Q: What happens if you run an UPDATE without a WHERE clause?
A: Every row in the table is updated with the new value(s). That's why you should always write the WHERE, test it with a SELECT first, and ideally run the change inside a transaction so you can ROLLBACK if the rows-affected count looks wrong.
Key points to remember
- UPDATE changes values in existing rows; it never adds or removes rows.
- Syntax: UPDATE table SET col1 = v1, col2 = v2 WHERE condition;
- SET lists the new values — separate multiple columns with commas, not AND.
- WHERE chooses which rows change; without WHERE, EVERY row is updated.
- New values can be calculated from old ones: SET runs = runs + 50.
- Filter by the primary key (WHERE id = 4) to target exactly one row.
- SQL Server can also UPDATE using a JOIN with UPDATE alias SET … FROM … JOIN.
Try it yourself
Change one value: Dhoni 350 → 400
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Change many columns: Warner to SRH with 760 runs
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
DANGER: UPDATE without WHERE
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Calculated update using another table
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: Kohli's big season
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
