Using Subqueries in the WHERE Clause of UPDATE Statements in SQL
Last Updated :
21 Jun, 2025
A subquery is a query that is used inside another SQL query. It can be used to return a single value or a set of values that will be used by the outer query. Subqueries in the WHERE clause are particularly useful when we need to filter records based on conditions that involve other tables or more complex criteria. Basic syntax for using a subquery within the WHERE clause of an UPDATE statement is given below:
Syntax:
UPDATE TableName
SET column_name = new_value
WHERE column_name IN (SELECT column_name FROM another_table or same_table WHERE condition);
Here,
- TableName: The table that contains the records to be updated.
- column_name: The column to be updated in the target table.
- new_value: The new value to assign to the column.
- The subquery: A nested query that selects values used to filter which rows to update.
Examples of Using Subqueries in the WHERE Clause of SQL UPDATE
To understand How to perform subqueries in the WHERE Clause of UPDATE statement in SQL, We will consider two table called Employees, Departments on which we will perform various operations and queries. as shown below:
1. Employees Table
Employees Table2. Department Table
Department TableExample 1: Update Records Based on a Condition from Another Table
Suppose we want to update the salary of employees in the Sales department to match the average salary defined in the Departments table for that department.
Query:
UPDATE Employees
SET Salary = (SELECT AvgSalary FROM Departments WHERE Department = 'Sales')
WHERE Department = 'Sales';
Output
Example1Explanation:
- The subquery (SELECT AvgSalary FROM Departments WHERE Department = 'Sales') finds the average salary for the "Sales" department.
- The UPDATE statement sets the Salary of employees in the Sales department to this average salary.
- The WHERE clause ensures that only employees in the "Sales" department are updated.
Example 2: Increase the Salary of Employees
Suppose we want to increase the salary of employees by 10% in their respective departments for those who earn less than the average salary of their department. We can write the following query using a subquery in the WHERE clause:
Query:
UPDATE Employees
SET Salary = Salary * 1.10
WHERE Salary < (SELECT AVG(Salary) FROM Employees WHERE Department = Employees.Department);
Output
Example 2Explanation:
- The subquery (SELECT AVG(Salary) FROM Employees WHERE Department = Employees.Department) calculates the average salary for each department.
- The UPDATE statement increases the salary by 10% for employees who earn less than the average salary in their department.
Example 3: Update with a Subquery to Modify Data Based on Multiple Conditions
Suppose we want to update the Salary of employees who have a salary lower than the average salary of their respective departments. This will involve a correlated subquery because the inner query will reference the DepartmentID
from the outer query.
Query:
UPDATE Employees
SET Salary = Salary * 1.15
WHERE Salary < (SELECT AVG(Salary)
FROM Employees
WHERE Employees.DepartmentID = Employees.DepartmentID);
Output
EmployeeID | Name | DepartmentID | Department | Salary |
---|
1 | Alice | 1 | Sales | 57500.00 |
2 | Bob | 2 | IT | 60000.00 |
3 | Charlie | 1 | Sales | 55000.00 |
4 | David | 3 | HR | 45000.00 |
Explanation:
- The subquery
(SELECT AVG(Salary) FROM Employees WHERE Employees.DepartmentID = Employees.DepartmentID)
computes the average salary for each employee's department. - The outer query then updates the salary of employees whose salary is lower than the computed average salary for their department.
Important Points to Remember
- Subqueries in the WHERE Clause of an update statement allow for conditional updates based on more complex conditions.
- We can use scalar subqueries or correlated subqueries depending on the problem.
- Subqueries can be used with comparison operators like
=
, IN
, BETWEEN
, and others to compare the result of the subquery with columns in the outer query. - A correlated subquery is especially useful when the inner query needs to reference values from the outer query, making it more dynamic.
Conclusion
Overall, Using subqueries in the WHERE clause of an UPDATE statement is a powerful technique to conditionally update records in SQL. Whether you are updating based on values from another table or from within the same table, subqueries allow for more complex and flexible data manipulation. By understanding how to utilize subqueries effectively, you can write more efficient and readable SQL queries.
Similar Reads
How to Update Table Rows in SQLite Using Subquery SQLite is a lightweight, serverless RDBMS that is used to store and manipulate the data in the database in relational or tabular form. It can run on various operating systems like Windows, macOS, Linux and mobile operating systems like Android and iOS. SQLite is designed for single-user access. Mult
4 min read
How to Update Table Rows in PL/SQL Using Subquery? Updating table rows in PL/SQL via subqueries allows precise data modifications based on specific conditions. By integrating subqueries within the UPDATE statement, rows can be selectively targeted for updates, enhancing data management efficiency. This article explores the concept of updating rows i
4 min read
How to Update Table Rows in SQL Server using Subquery ? Updating table rows in SQL Server using a subquery is a common operation that allows us to modify records based on values derived from another table or query. In this article, we will explain how to update table rows in SQL Server using subquery with the help of examples. Updating Table Rows Using a
3 min read
When to Use "ON UPDATE CASCADE" in PostgreSQL? In PostgreSQL, the ON UPDATE CASCADE clause in a foreign key constraint allows for a cascading update behavior between linked tables. This feature automatically updates all matching values in the referencing columns of child tables when a value is changed in the referenced column of the parent table
4 min read
How to Update Table Rows Using Subquery in MySQL Updating table rows using subqueries in MySQL enables precise modifications based on specific conditions or values from other tables. This technique leverages subqueries within the SET or WHERE clauses of the UPDATE statement, allowing dynamic and context-specific updates. This guide covers the synt
5 min read
How to Update Two Tables in One Statement in SQL Server? To update two tables in one statement in SQL Server, use the BEGIN TRANSACTION clause and the COMMIT clause. The individual UPDATE clauses are written in between the former ones to execute both updates simultaneously. Here, we will learn how to update two tables in a single statement in SQL Server.
3 min read