Updating Data with SQL UPDATE

In SQL, the UPDATE statement is used to modify existing data in a table. It is one of the most commonly used SQL commands, allowing you to change one or more fields of one or more records. The UPDATE command is essential for managing data that evolves over time, making it a powerful tool for database maintenance and real-time data management.

This chapter will walk you through everything you need to know about the UPDATE statement, starting from the basics and progressing to more advanced concepts. We will also cover how to update multiple rows, handle conditional updates, use subqueries, and prevent potential pitfalls like unintended mass updates.

Basic Syntax of the UPDATE Statement

The basic structure of the UPDATE statement is as follows:

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

				
			
  • table_name: The table in which the data needs to be updated.
  • SET: Specifies which columns should be updated and their new values.
  • WHERE: The condition that determines which rows to update. If you omit the WHERE clause, all rows in the table will be updated (which can be dangerous if not done intentionally).

Example 1: Updating a Single Row

Consider a table employees:

IDNameDepartmentSalary
1John DoeHR5000
2Jane SmithIT6000
3David GreenSales5500

Let’s say we want to update John Doe’s salary to 5500.

Query:

				
					UPDATE employees
SET salary = 5500
WHERE id = 1;

				
			

Output (after running the query):

IDNameDepartmentSalary
1John DoeHR5500
2Jane SmithIT6000
3David GreenSales5500

Only the salary of John Doe was updated because the WHERE clause restricted the update to the row where id = 1.

Updating Multiple Rows

You can update multiple rows at once by changing the WHERE condition to match more than one record. This is useful when you want to update a group of rows that share certain characteristics.

Example 2: Updating Multiple Rows

Let’s give a salary raise of 500 to all employees in the IT department.

Query:

				
					UPDATE employees
SET salary = salary + 500
WHERE department = 'IT';

				
			

Output:

IDNameDepartmentSalary
1John DoeHR5500
2Jane SmithIT6500
3David GreenSales5500

Both employees in the IT department had their salaries increased by 500.

Using the WHERE Clause with UPDATE

The WHERE clause in the UPDATE statement is critical because it determines which rows get updated. Without it, all rows in the table will be modified, which may lead to unintended consequences.

Example 3: Omitting the WHERE Clause

If we mistakenly omit the WHERE clause, the query will update all rows in the table.

Query (Dangerous):

				
					UPDATE employees
SET salary = 7000;

				
			

Output:

IDNameDepartmentSalary
1John DoeHR7000
2Jane SmithIT7000
3David GreenSales7000

All employees now have a salary of 7000. This is likely not what was intended, highlighting the importance of using the WHERE clause correctly.

Conditional Updates Using AND/OR

You can combine multiple conditions in the WHERE clause using AND or OR operators to fine-tune which rows are updated.

Example 4: Combining Conditions with AND

Let’s say we want to give a salary increment to employees in the Sales department who currently have a salary less than 6000.

Query:

				
					UPDATE employees
SET salary = salary + 500
WHERE department = 'Sales' AND salary < 6000;


				
			

Output:

IDNameDepartmentSalary
1John DoeHR7000
2Jane SmithIT7000
3David GreenSales6000

Only David Green’s salary was updated because he is in Sales and his salary was less than 6000.

Example 5: Combining Conditions with OR

Let’s update the salary of any employee who is either in HR or has a salary greater than 6000.

Query:

				
					UPDATE employees
SET salary = salary + 300
WHERE department = 'HR' OR salary > 6000;

				
			

Output:

IDNameDepartmentSalary
1John DoeHR7300
2Jane SmithIT7300
3David GreenSales6000

John Doe’s salary was updated because he is in HR, and Jane Smith’s salary was updated because her salary is greater than 6000.

Updating Data with Subqueries

Sometimes, you may need to update a table based on values from another table. In such cases, you can use subqueries in the UPDATE statement.

Example 6: Using a Subquery in UPDATE

Assume we have another table, performance_reviews, that stores performance ratings for each employee:

Employee IDRating
14.5
23.7
34.2

Let’s give a 10% raise to employees with a performance rating of 4.0 or higher.

Query:

				
					UPDATE employees
SET salary = salary * 1.10
WHERE id IN (SELECT employee_id FROM performance_reviews WHERE rating >= 4.0);

				
			

Output:

IDNameDepartmentSalary
1John DoeHR8030
2Jane SmithIT7300
3David GreenSales6600

Both John Doe and David Green received a 10% salary raise because their performance ratings were 4.0 or higher.

Updating with Joins

SQL also allows you to use JOIN clauses in UPDATE queries. This is helpful when you need to update a table based on data from another table.

Example 7: UPDATE with JOIN

Let’s say we want to update the salary of employees based on their department’s average salary, stored in another table called department_salaries:

DepartmentAvg Salary
HR6000
IT7000
Sales6500

We want to align each employee’s salary to their department’s average salary.

Query:

				
					UPDATE employees e
JOIN department_salaries ds ON e.department = ds.department
SET e.salary = ds.avg_salary;

				
			

Output:

IDNameDepartmentSalary
1John DoeHR6000
2Jane SmithIT7000
3David GreenSales6500

Each employee’s salary was updated to match their department’s average salary.

Updating Multiple Tables

Some database systems (such as MySQL) allow updating multiple tables in a single query, but this functionality is limited. It’s more common to use multiple UPDATE statements or transactions when you need to modify more than one table at a time.

Example 8: Updating Multiple Tables

				
					UPDATE employees e, department_salaries ds
SET e.salary = ds.avg_salary
WHERE e.department = ds.department;

				
			

This query updates both tables, but the syntax and support vary by database system.

Best Practices for UPDATE Statements

Use Transactions for Critical Updates

  • When performing critical updates that could affect a large number of rows, always use transactions to ensure that changes can be rolled back in case of an error.
				
					START TRANSACTION;
UPDATE employees SET salary = salary * 1.10 WHERE department = 'Sales';
COMMIT;

				
			

Test Updates Before Running Them

  • It’s good practice to run a SELECT query with the same WHERE condition first to verify that the correct rows will be updated.

				
					SELECT * FROM employees WHERE department = 'Sales';

				
			

Backup Your Data

  • Always back up your data before performing mass updates, especially when dealing with production databases.

Avoid Unintended Mass Updates

  • Double-check your WHERE clause to avoid updating the entire table by mistake.

The UPDATE statement is a vital tool in SQL for modifying existing data in a table. Whether you're updating a single row or multiple rows, using conditions or subqueries, or even joining tables to perform updates, understanding how to effectively use the UPDATE command is key to maintaining and manipulating data efficiently. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India