0
0
MySQLquery~10 mins

UPDATE with WHERE clause in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - UPDATE with WHERE clause
Start UPDATE statement
Check WHERE condition for each row
Update row
End UPDATE
The UPDATE command checks each row against the WHERE condition. If true, it updates that row; if false, it leaves it unchanged.
Execution Sample
MySQL
UPDATE employees
SET salary = salary + 500
WHERE department = 'Sales';
This query increases salary by 500 only for employees in the Sales department.
Execution Table
StepRow IDCurrent salaryDepartmentWHERE condition (department='Sales')ActionNew salary
113000SalesTrueUpdate salary3500
223200HRFalseSkip row3200
332800SalesTrueUpdate salary3300
444000ITFalseSkip row4000
553100SalesTrueUpdate salary3600
6----End of table-
💡 All rows checked; only rows with department 'Sales' updated.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5Final
salary_row_13000350035003500350035003500
salary_row_23200320032003200320032003200
salary_row_32800280028003300330033003300
salary_row_44000400040004000400040004000
salary_row_53100310031003100310036003600
Key Moments - 3 Insights
Why are some rows not updated even though the UPDATE command runs?
Rows not matching the WHERE condition are skipped, as shown in execution_table rows 2 and 4 where the department is not 'Sales'.
Does the UPDATE affect all rows if WHERE is omitted?
No WHERE means all rows match condition by default, so all rows would be updated. Here, only rows with 'Sales' are updated.
What happens if the WHERE condition is always false?
No rows get updated. The execution_table would show all rows skipped, similar to rows 2 and 4 here.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the new salary of row 3 after the update?
A3300
B2800
C3500
D3100
💡 Hint
Check the 'New salary' column for Row ID 3 in the execution_table.
At which step does the WHERE condition evaluate to false for the first time?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'WHERE condition' column in execution_table rows.
If the WHERE clause was removed, how would the action column change in the execution_table?
AAll rows would show 'Skip row'.
BAll rows would show 'Update salary'.
COnly rows with salary > 3000 would update.
DNo rows would be updated.
💡 Hint
Without WHERE, all rows match condition by default, so all get updated.
Concept Snapshot
UPDATE table_name
SET column = value
WHERE condition;

- Only rows matching WHERE condition are updated.
- Rows not matching remain unchanged.
- Omitting WHERE updates all rows.
Full Transcript
The UPDATE statement changes data in a table. It checks each row against the WHERE condition. If the condition is true, it updates that row's columns as specified. If false, it leaves the row unchanged. For example, increasing salary by 500 only for employees in the Sales department updates only those rows. Rows in other departments are skipped. This selective update prevents unwanted changes. If WHERE is omitted, all rows get updated. This trace shows step-by-step how each row is checked and updated or skipped.