0
0
SQLquery~10 mins

Why UPDATE needs caution in SQL - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why UPDATE needs caution
Start UPDATE command
Check WHERE condition
Update row
Repeat for next row
End UPDATE
The UPDATE command checks each row against the WHERE condition. If true, it updates the row; if false, it skips. This repeats for all rows.
Execution Sample
SQL
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales';
This increases salary by 10% only for employees in the Sales department.
Execution Table
StepRow IDCurrent salaryCondition (department='Sales')ActionNew salary
110150000TrueUpdate salary55000
210260000FalseSkip60000
310355000TrueUpdate salary60500
410470000FalseSkip70000
510548000TrueUpdate salary52800
6-----
Exit--No more rowsStop-
💡 All rows checked; UPDATE ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
salary_10150000550005500055000550005500055000
salary_10260000600006000060000600006000060000
salary_10355000550006050060500605006050060500
salary_10470000700007000070000700007000070000
salary_10548000480004800048000528005280052800
Key Moments - 3 Insights
Why is the WHERE clause important in UPDATE?
Without WHERE, all rows update (see execution_table rows 2 and 4 where condition is False and rows are skipped). This can cause unintended changes.
What happens if the condition is always True?
All rows get updated, which might not be desired. The exit_note and execution_table show UPDATE runs on every row if condition is True.
Why do some rows keep their original salary?
Because their department is not 'Sales', so condition is False and action is Skip (rows 2 and 4). This shows selective updating.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the new salary of row ID 103 after step 3?
A55000
B60500
C60000
D70000
💡 Hint
Check row 3 in execution_table under 'New salary' column.
At which step does the UPDATE skip a row due to the condition being False?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at execution_table rows where 'Condition' is False and 'Action' is Skip.
If the WHERE clause was removed, what would happen to the salaries?
AOnly Sales department salaries increase
BNo salaries change
CAll salaries increase by 10%
DOnly non-Sales salaries increase
💡 Hint
Refer to key_moments about importance of WHERE clause and execution_table showing selective updates.
Concept Snapshot
UPDATE changes data in rows matching a condition.
Always use WHERE to limit rows updated.
Without WHERE, all rows change.
Check condition carefully to avoid mistakes.
Test UPDATE with SELECT first.
Full Transcript
The UPDATE command changes data in a table. It checks each row against the WHERE condition. If the condition is true, it updates that row; if false, it skips it. This repeats for all rows. Without a WHERE clause, UPDATE changes every row, which can cause big mistakes. In the example, only employees in the Sales department get a 10% salary increase. Rows not in Sales keep their salary. This shows why UPDATE needs caution: to avoid changing unintended rows, always use a correct WHERE condition.