T
Tech Town
Log InSign Up Free
← Back to Blog

June 17, 2025 · admin

UPDATE

πŸ”„ SQL UPDATE Statement – How to Modify Data in SQL Tables

Need to correct or change existing data in your SQL database? The UPDATE statement is your go-to command for modifying values without deleting or recreating rows.

This tutorial walks you through the SQL UPDATE syntax, usage patterns, best practices, and real-world examples to help you confidently update your data.


πŸ“˜ What is the SQL UPDATE Statement?

The UPDATE command in SQL is used to modify existing records in a table. You can update a single column, multiple columns, one row, or several rows at once based on a condition.


🧾 SQL UPDATE Syntax

UPDATE table_name
SET column1 = value1,
    column2 = value2,
    ...
WHERE condition;

⚠️ Always use the WHERE clause unless you want to update all rows in the table!


βœ… Example: Update a Single Row

UPDATE employees
SET salary = 85000
WHERE emp_id = 101;

This updates the salary of the employee with ID 101.


βœ… Example: Update Multiple Columns

UPDATE employees
SET emp_name = 'Neha Singh',
    department = 'Finance'
WHERE emp_id = 102;

⚠️ Update All Rows (Use With Caution!)

UPDATE employees
SET bonus = 5000;

This adds a bonus to every employee in the table. Useful in some cases, but risky if done by mistake!


🧠 Use Conditional Expressions in UPDATE

You can use conditional logic like CASE:

UPDATE employees
SET bonus = CASE
    WHEN department = 'Sales' THEN 10000
    WHEN department = 'HR' THEN 5000
    ELSE 3000
END;

πŸ”„ Update Using a Subquery

UPDATE employees
SET salary = (
    SELECT AVG(salary)
    FROM employees
    WHERE department = 'IT'
)
WHERE department = 'Marketing';

πŸ” Best Practices for SQL UPDATE

  • βœ… Always test your query with a SELECT statement first to preview affected rows.
  • βœ… Use transactions (BEGIN, ROLLBACK, COMMIT) if making bulk updates.
  • 🚨 Never run an UPDATE without a WHERE clause unless it’s intentional.
  • πŸ›‘οΈ Backup critical data before large updates.
  • πŸ§ͺ Use staging/test environments before production updates.

🎯 Real-Life Use Cases

Use CaseExample SQL
Update salary after annual reviewSET salary = salary * 1.10
Change department for an employeeSET department = 'R&D' WHERE emp_id = 105
Reset login attempts after password changeSET login_attempts = 0
Update prices in an e-commerce catalogSET price = price * 1.05

πŸ“ Summary

  • UPDATE changes one or more values in an existing row.
  • It supports conditional updates, multi-column updates, and subqueries.
  • Always use WHERE to target specific rows.
  • One of the most powerful tools in your SQL toolkit for data maintenance.

πŸ“š Recommended Next Reads

  • 🧱 SQL INSERT – Learn how to add new records
  • 🧹 SQL DELETE – Remove specific rows safely
  • πŸ”Ž SQL SELECT – Query the data you’re updating
  • πŸ“‹ SQL WHERE Clause – Master conditional filtering