UPDATE single column in SQL - Time & Space Complexity
When we update a single column in a database table, it is important to understand how the time taken changes as the table grows.
We want to know how the number of rows affects the time it takes to perform the update.
Analyze the time complexity of the following SQL update statement.
UPDATE employees
SET salary = salary * 1.05
WHERE department = 'Sales';
This code increases the salary by 5% for all employees in the Sales department.
Look for repeated actions that affect time.
- Primary operation: Checking each row to see if it belongs to the Sales department.
- How many times: Once for every row in the employees table.
As the number of employees grows, the database must check more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of rows.
Time Complexity: O(n)
This means the time to update grows in a straight line as the table gets bigger.
[X] Wrong: "Updating one column is always very fast and does not depend on table size."
[OK] Correct: Even if only one column changes, the database must still check each row to find which ones to update, so time grows with table size.
Understanding how updates scale helps you explain database behavior clearly and shows you know what affects performance in real projects.
"What if we add an index on the department column? How would the time complexity change?"
Practice
UPDATE employees SET salary = 5000 WHERE id = 3; do?Solution
Step 1: Understand the UPDATE statement
The UPDATE command changes existing data in a table.Step 2: Analyze the SET and WHERE clauses
SET salary = 5000 changes the salary column, WHERE id = 3 limits the change to the employee with id 3.Final Answer:
It changes the salary to 5000 for the employee with id 3. -> Option AQuick Check:
UPDATE with SET and WHERE changes specified rows [OK]
- Thinking UPDATE adds new rows
- Forgetting WHERE updates all rows
- Confusing UPDATE with SELECT
price column to 20 in the products table for product with id 10?Solution
Step 1: Check the UPDATE syntax
The correct syntax is UPDATE table_name SET column = value WHERE condition;Step 2: Validate each option
UPDATE products SET price = 20 WHERE id = 10; matches the correct syntax with SET and WHERE clauses properly used.Final Answer:
UPDATE products SET price = 20 WHERE id = 10; -> Option AQuick Check:
Correct UPDATE syntax uses SET column = value and WHERE [OK]
- Omitting SET keyword
- Placing WHERE before SET
- Missing semicolon at end
students with columns id, name, and grade, what will be the result of this query?UPDATE students SET grade = 90 WHERE name = 'Alice';
Assuming the table before update:
id | name | grade 1 | Alice | 85 2 | Bob | 88 3 | Alice | 92
Solution
Step 1: Understand the WHERE clause effect
WHERE name = 'Alice' matches all rows where name is exactly 'Alice'.Step 2: Apply UPDATE to matching rows
Both rows with name 'Alice' (id 1 and 3) will have their grade set to 90.Final Answer:
Both rows with name 'Alice' will have grade updated to 90. -> Option CQuick Check:
UPDATE affects all rows matching WHERE condition [OK]
- Assuming only one row updates with non-unique WHERE
- Thinking UPDATE without WHERE updates one row
- Confusing UPDATE with SELECT
UPDATE orders SET status 'shipped' WHERE order_id = 101;
Solution
Step 1: Check SET clause syntax
SET clause must have column = value format. Here, equal sign is missing.Step 2: Verify other parts
WHERE clause is present, table name looks correct, and no quotes around column name.Final Answer:
Missing equal sign (=) between column and value. -> Option BQuick Check:
SET requires column = value syntax [OK]
- Omitting equal sign in SET
- Forgetting WHERE causes all rows update
- Using quotes incorrectly around column names
inventory with columns item_id, quantity, and price. You want to increase the price by 10% only for items with quantity less than 5. Which SQL statement correctly updates the price column?Solution
Step 1: Understand the requirement
Increase price by 10% means multiply price by 1.10.Step 2: Check the WHERE condition
Only update rows where quantity is less than 5.Step 3: Evaluate each option
UPDATE inventory SET price = price * 1.10 WHERE quantity < 5; correctly multiplies price by 1.10 with proper WHERE clause.Final Answer:
UPDATE inventory SET price = price * 1.10 WHERE quantity < 5; -> Option DQuick Check:
Multiply price by 1.10 to increase by 10% [OK]
- Adding 10 instead of 10%
- Multiplying by 0.10 reduces price
- Adding 0.10 increases price by 0.10 units only
Ready to prove it?
Pass the test and this pattern moves unknown
