๐ ๏ธ SQL UPDATE โ Modify Existing Records in Tables
๐งฒ Introduction โ Why Use SQL UPDATE?
The UPDATE statement in SQL is used to modify existing data in a table. It allows you to change one or more column values for rows that match a specific condition.
Without UPDATE, youโd have to delete and re-insert records โ a far less efficient method.
๐ฏ In this guide, youโll learn how to:
- Update a single row or multiple rows
- Use
WHEREto control which records are updated - Update based on calculations or subqueries
- Use
UPDATEwith joins
โ 1. Basic UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
โ
Always include WHERE to avoid changing all rows.
๐งพ 2. Update a Single Record
UPDATE employees
SET salary = 60000
WHERE id = 101;
โ
Modifies the salary for the employee with ID 101.
๐ 3. Update Multiple Records
UPDATE products
SET status = 'inactive'
WHERE quantity = 0;
โ Updates all rows where the condition matches.
๐งฎ 4. Update with Expressions
UPDATE employees
SET salary = salary * 1.05
WHERE department = 'Sales';
โ Increases salary by 5% for all Sales employees.
๐ 5. Update Using a Subquery
UPDATE employees
SET department = 'Operations'
WHERE id IN (SELECT employee_id FROM transfers);
โ Uses data from another table to filter rows.
๐ 6. Update with JOIN (varies by SQL dialect)
-- MySQL / PostgreSQL
UPDATE employees
SET employees.salary = new_salaries.salary
FROM new_salaries
WHERE employees.id = new_salaries.id;
โ Useful for updating based on related table data.
โ ๏ธ 7. Update All Rows (Use Carefully)
UPDATE products
SET discount = 10;
โ ๏ธ Applies the same change to every row โ use only if intended.
๐ Best Practices
| โ Do This | โ Avoid This |
|---|---|
Always use a WHERE clause | Updating all rows by mistake |
| Backup before bulk updates | Skipping transactions on large data |
Test your query with SELECT | Assuming UPDATE affects one row |
| Use JOINs and subqueries wisely | Updating without validating results |
๐ Summary โ Recap & Next Steps
The UPDATE command is essential for modifying existing records safely and efficiently. From adjusting salaries to bulk status changes, UPDATE gives you precise control.
๐ Key Takeaways:
- Use
UPDATEwithSETto change values - Always add a
WHEREclause to scope the update - Can use expressions, subqueries, and joins
- Use transactions when modifying large datasets
โ๏ธ Real-World Relevance:UPDATE is used in inventory management, account maintenance, audit logs, and operational workflows.
โก๏ธ Next: Learn how to remove data using SQL DELETE.
โ FAQ โ SQL UPDATE
โ Can I update multiple columns at once?
โ
Yes. Separate them with commas in the SET clause.
โ What if I forget the WHERE clause?
โ ๏ธ All rows in the table will be updated. Always double-check.
โ Can I use a subquery in an UPDATE?
โ Yes, subqueries are often used to filter which rows are updated.
โ Can I undo an UPDATE?
โ
Only if youโve used a TRANSACTION and havenโt committed yet. Otherwise, no.
โ Can I update data from another table?
โ Yes, using joins or subqueries depending on your SQL dialect.
Share Now :
