UPDATE with expressions in SQL - Time & Space Complexity
When we update data in a table using expressions, it is important to know how the time to complete the update changes as the table grows.
We want to understand how the number of rows affects the work the database does during the update.
Analyze the time complexity of the following code snippet.
UPDATE employees
SET salary = salary * 1.05
WHERE department = 'Sales';
This code increases the salary by 5% for all employees in the Sales department.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database scans rows to find those in the Sales department.
- How many times: It checks each row once to see if it matches the condition, then updates matching rows.
As the number of rows grows, the database must check more rows to find those to update.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and some updates |
| 100 | About 100 checks and more updates |
| 1000 | About 1000 checks and many updates |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to complete the update grows linearly with the number of rows in the table.
[X] Wrong: "The update only changes a few rows, so it runs in constant time."
[OK] Correct: Even if only a few rows are updated, the database still checks every row to find which ones to update, so the time grows with the table size.
Understanding how updates scale helps you explain database behavior clearly and shows you can think about efficiency in real situations.
"What if we added an index on the department column? How would the time complexity change?"
Practice
UPDATE employees SET salary = salary + 500 do?Solution
Step 1: Understand the UPDATE statement
The statement updates the 'salary' column in the 'employees' table.Step 2: Analyze the expression used
It adds 500 to the current salary value for every row because no WHERE clause limits the rows.Final Answer:
Increases the salary of all employees by 500 -> Option BQuick Check:
salary = salary + 500 [OK]
- Thinking it sets salary to 500 instead of adding
- Confusing UPDATE with DELETE
- Assuming it adds 500 only to some rows without WHERE
Solution
Step 1: Identify correct expression for 10% increase
Multiplying price by 1.10 increases it by 10%.Step 2: Check WHERE clause for category filtering
WHERE category = 'Books' limits update to only books.Final Answer:
UPDATE products SET price = price * 1.10 WHERE category = 'Books'; -> Option AQuick Check:
price * 1.10 with WHERE category = 'Books' [OK]
- Using + 10% which is invalid syntax
- Adding 0.10 instead of 10%
- Multiplying price by 10 instead of 1.10
accounts with columns id, balance, what will be the balances after running:UPDATE accounts SET balance = balance - 100 WHERE balance >= 500;Initial balances: 400, 500, 600
Solution
Step 1: Identify which rows meet the WHERE condition
Only balances 500 and 600 are >= 500, so only these rows update.Step 2: Subtract 100 from qualifying balances
500 - 100 = 400, 600 - 100 = 500; 400 remains unchanged.Final Answer:
[400, 400, 500] -> Option DQuick Check:
Only balances >= 500 reduced by 100 [OK]
- Updating all rows ignoring WHERE
- Subtracting 100 from all balances
- Confusing >= with >
UPDATE orders SET total = total + discount WHERE discount;Solution
Step 1: Analyze the WHERE clause
WHERE discount is incomplete; it needs a condition like discount > 0.Step 2: Check other parts of the statement
Table name and SET expression are correct; updating with another column is valid.Final Answer:
The WHERE clause is invalid because it lacks a comparison -> Option CQuick Check:
WHERE must have a condition, not just a column name [OK]
- Assuming column names alone are valid WHERE conditions
- Thinking SET cannot use other columns
- Ignoring missing comparison in WHERE
Solution
Step 1: Use a subquery to get average salary
The subquery (SELECT AVG(salary) FROM employees) returns the average salary.Step 2: Apply 5% increase only to salaries below average
WHERE salary < average filters employees below average; salary * 1.05 increases salary by 5%.Final Answer:
UPDATE employees SET salary = salary * 1.05 WHERE salary < (SELECT AVG(salary) FROM employees); -> Option AQuick Check:
Use subquery in WHERE and multiply salary by 1.05 [OK]
- Using AVG(salary) without subquery
- Adding 5 instead of 5%
- Updating salaries above average instead of below
- Multiplying by 0.95 which decreases salary
Ready to prove it?
Pass the test and this pattern moves unknown
