๐Ÿ“„ SQL Basics โ€“ Core Queries & Clauses
Estimated reading: 3 minutes 40 views

๐Ÿ› ๏ธ 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 WHERE to control which records are updated
  • Update based on calculations or subqueries
  • Use UPDATE with 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 clauseUpdating all rows by mistake
Backup before bulk updatesSkipping transactions on large data
Test your query with SELECTAssuming UPDATE affects one row
Use JOINs and subqueries wiselyUpdating 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 UPDATE with SET to change values
  • Always add a WHERE clause 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ› ๏ธ SQL UPDATE

Or Copy Link

CONTENTS
Scroll to Top