UPDATE without WHERE (danger) in SQL - Time & Space Complexity
When we run an UPDATE command without a WHERE clause, it changes every row in the table.
We want to understand how the time it takes grows as the table gets bigger.
Analyze the time complexity of the following code snippet.
UPDATE employees
SET salary = salary * 1.05;
This code increases the salary by 5% for every employee in the table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database updates each row one by one.
- How many times: Once for every row in the table.
As the number of rows grows, the work grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: The number of operations grows directly with the number of rows.
Time Complexity: O(n)
This means the time to complete the update grows in direct proportion to the number of rows.
[X] Wrong: "Updating without WHERE is fast because it's just one command."
[OK] Correct: Even though it's one command, the database must still update every row, so the time grows with the table size.
Understanding how updates scale helps you write safer and more efficient database commands in real projects.
"What if we add a WHERE clause that updates only 10 rows? How would the time complexity change?"
Practice
What happens if you run an UPDATE statement without a WHERE clause?
Solution
Step 1: Understand the role of WHERE clause in UPDATE
TheWHEREclause limits which rows are updated. Without it, no filter is applied.Step 2: Effect of missing WHERE clause
WithoutWHERE, the update affects every row in the table.Final Answer:
All rows in the table will be updated. -> Option AQuick Check:
UPDATE without WHERE updates all rows [OK]
- Thinking only one row updates without WHERE
- Assuming syntax error occurs without WHERE
- Believing no rows are updated without WHERE
Which of the following is the correct syntax to update the salary column to 5000 for all employees in the employees table?
UPDATE employees SET salary = 5000;
Solution
Step 1: Identify correct UPDATE syntax
The correct syntax isUPDATE table_name SET column = value;to update all rows.Step 2: Check each option
UPDATE employees SET salary = 5000; matches correct syntax without WHERE, updating all rows. Others have syntax errors or extra clauses.Final Answer:
UPDATE employees SET salary = 5000; -> Option DQuick Check:
UPDATE table SET column = value; updates all rows [OK]
- Omitting SET keyword
- Using WHERE when not needed for all rows
- Incorrect order of clauses
Given the table products with 3 rows:
id | name | price ---+---------+------- 1 | Pen | 10 2 | Pencil | 5 3 | Eraser | 3
What will be the result after running this query?
UPDATE products SET price = price + 2;
Solution
Step 1: Understand UPDATE without WHERE effect
Without aWHEREclause, the update applies to all rows in the table.Step 2: Apply the price update
Each product's price increases by 2, so prices become 12, 7, and 5 respectively.Final Answer:
All products' prices increase by 2. -> Option BQuick Check:
UPDATE without WHERE updates all rows [OK]
- Thinking only one row updates
- Assuming missing WHERE causes no update
- Expecting syntax error without WHERE
Consider this query intended to update only employees in department 10:
UPDATE employees SET salary = salary + 1000;
Why might this be a problem?
Solution
Step 1: Identify missing WHERE clause
The query lacks aWHEREclause to filter employees by department.Step 2: Effect of missing WHERE
Without filtering, the update applies to all employees, not just those in department 10.Final Answer:
It will update salaries for all employees, not just department 10. -> Option AQuick Check:
Missing WHERE updates all rows, causing unintended changes [OK]
- Assuming syntax error without WHERE
- Thinking no rows update without WHERE
- Believing only one row updates by default
You want to increase the stock by 10 only for products with stock less than 5. Which query correctly avoids the danger of updating all rows?
Solution
Step 1: Identify the condition to limit update
We want to update only rows wherestock < 5, so aWHEREclause is needed.Step 2: Check query correctness
UPDATE products SET stock = stock + 10 WHERE stock < 5;uses correct syntax and condition to update only matching rows.UPDATE products SET stock = stock + 10;(no WHERE) updates all rows.UPDATE products stock = stock + 10 WHERE stock < 5;(missing SET) has syntax error.UPDATE products SET stock = stock + 10 WHERE stock > 5;updates wrong rows (opposite condition).Final Answer:
UPDATE products SET stock = stock + 10 WHERE stock < 5; -> Option CQuick Check:
Use WHERE to limit UPDATE to desired rows [OK]
- Omitting WHERE and updating all rows
- Using wrong comparison operator
- Syntax errors by missing SET keyword
Ready to prove it?
Pass the test and this pattern moves unknown
