UPDATE multiple columns in SQL - Time & Space Complexity
When we update multiple columns in a database table, it is important to understand how the time it takes grows as the table gets bigger.
We want to know how the work changes when there are more rows to update.
Analyze the time complexity of the following code snippet.
UPDATE employees
SET salary = salary * 1.05,
bonus = bonus + 1000
WHERE department = 'Sales';
This code updates the salary and bonus columns for all employees in the Sales department.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database scans each row to check if it belongs to the Sales department and then updates the columns.
- How many times: This happens once for each row in the table, but only rows matching the condition are updated.
As the number of rows grows, the database must check more rows to find those in the Sales department.
| 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 update grows linearly with the number of rows in the table.
[X] Wrong: "Updating multiple columns takes more time than updating one column for each row."
[OK] Correct: The main cost is finding the rows to update. Changing more columns in those rows adds little extra time compared to scanning the rows.
Understanding how updates scale helps you explain database performance clearly and shows you know what affects query speed.
"What if we removed the WHERE clause and updated all rows? How would the time complexity change?"
Practice
UPDATE statement do when you specify multiple columns in the SET clause?Solution
Step 1: Understand the UPDATE statement
The UPDATE statement modifies existing rows in a table.Step 2: Effect of multiple columns in SET clause
When multiple columns are listed in SET, all those columns are updated in the rows that match the WHERE condition.Final Answer:
It changes the values of all specified columns in the selected rows. -> Option DQuick Check:
UPDATE with multiple columns updates all listed columns [OK]
- Thinking UPDATE adds new rows
- Believing UPDATE deletes rows
- Assuming only first column updates
price and stock, in a table named products?Solution
Step 1: Recall correct UPDATE syntax
The correct syntax is UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;Step 2: Check each option
UPDATE products SET price = 10, stock = 50 WHERE id = 1; matches the correct syntax exactly. The other options have syntax errors.Final Answer:
UPDATE products SET price = 10, stock = 50 WHERE id = 1; -> Option AQuick Check:
SET column = value pairs separated by commas [OK]
- Using parentheses incorrectly in SET clause
- Placing WHERE before SET
- Missing commas between column assignments
employees with columns salary and bonus, what will be the result of this query?UPDATE employees SET salary = salary + 1000, bonus = 500 WHERE department = 'Sales';
Solution
Step 1: Understand the WHERE clause effect
The WHERE clause limits updates to rows where department = 'Sales'.Step 2: Analyze SET clause changes
Salary increases by 1000 and bonus is set to 500 for those rows only.Final Answer:
All employees in Sales get their salary increased by 1000 and bonus set to 500. -> Option CQuick Check:
WHERE filters rows; SET updates all listed columns [OK]
- Ignoring WHERE and updating all rows
- Thinking bonus stays unchanged
- Believing multiple columns update causes error
UPDATE orders SET status = 'Shipped' stock = 0 WHERE order_id = 123;
Solution
Step 1: Check SET clause syntax
Multiple columns must be separated by commas in SET clause.Step 2: Identify missing comma
Between status = 'Shipped' and stock = 0, a comma is missing.Final Answer:
Missing comma between column assignments in SET clause. -> Option BQuick Check:
Separate columns with commas in SET [OK]
- Omitting commas between columns
- Placing WHERE before SET
- Using wrong quotes for strings
price and discount columns in the products table only for items where category is 'Electronics' and current stock is less than 10. Which query correctly does this?Solution
Step 1: Understand the condition requirements
We want to update only rows where category is 'Electronics' AND stock is less than 10.Step 2: Check each option's WHERE clause and syntax
UPDATE products SET price = price * 0.9, discount = 10 WHERE category = 'Electronics' AND stock < 10; uses AND with correct conditions and proper syntax. UPDATE products SET price = price * 0.9, discount = 10 WHERE category = 'Electronics' OR stock < 10; uses OR, which updates more rows. UPDATE products SET price = price * 0.9 discount = 10 WHERE category = 'Electronics' AND stock < 10; misses a comma between assignments. UPDATE products SET price = price * 0.9, discount = 10 WHERE category = 'Electronics' AND stock > 10; uses stock > 10, which is incorrect.Final Answer:
UPDATE products SET price = price * 0.9, discount = 10 WHERE category = 'Electronics' AND stock < 10; -> Option AQuick Check:
Use AND for combined conditions and commas between columns [OK]
- Using OR instead of AND in WHERE
- Missing commas between columns
- Wrong comparison operators in WHERE
Ready to prove it?
Pass the test and this pattern moves unknown
