What if you could fix many details with just one simple command instead of hours of typing?
Why UPDATE multiple columns in SQL? - Purpose & Use Cases
Imagine you have a big spreadsheet with customer info. You want to change both their phone number and address. Doing this by hand means opening each row and typing the new info one by one.
This manual way is slow and tiring. You might make mistakes, miss some rows, or spend hours updating just a few details. It's easy to lose track and get frustrated.
Using UPDATE multiple columns in SQL lets you change many pieces of information in one go. You write one simple command, and the database updates all the right spots quickly and correctly.
Change phone number, then change address separately for each customer.UPDATE customers SET phone = '123-4567', address = 'New Street 5' WHERE id = 10;
This lets you fix or improve many details at once, saving time and avoiding errors.
A store updates both the contact number and shipping address for a customer after they move, all with one simple command.
Manual updates are slow and error-prone.
UPDATE multiple columns changes many fields at once.
This saves time and keeps data accurate.
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
