Subqueries in the WHERE Clause of UPDATE in SQL
Last Updated :
19 Dec, 2024
In SQL, an UPDATE statement modifies existing records in a table. When updating records, we can use a subquery within the WHERE clause to conditionally update only specific rows based on results from another query.
In this article, we will learn how to write Subqueries in the WHERE Clause to Update the data in the SQL table in detail by understanding various examples.
What is a Subquery in SQL?
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.
Syntax:
Basic syntax for using a subquery within the WHERE clause of an UPDATE statement:
UPDATE TableName
SET column_name = new_value
WHERE column_name IN (SELECT column_name FROM another_table or same_table WHERE condition);
Explanation:
- 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 Subqueries in the WHERE Clause of UPDATE in SQL
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:
Employees Table:
Employees TableDepartment 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 of Updated Employees Table:
OutputExplanation:
- 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.
Query:
UPDATE Employees
SET Salary = Salary * 1.10
WHERE Salary < (SELECT AVG(Salary) FROM Employees WHERE Department = Employees.Department);
Output:
OutputExplanation:
- 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.
Benefits of Using Subqueries in the WHERE Clause of UPDATE
- Conditional Updates: Subqueries allow you to update records based on complex conditions that require data from other tables or computations within the same table.
- Increased Flexibility: Using a subquery provides flexibility by allowing you to filter rows based on aggregated results.
- Simplification: It simplifies the query when you need to filter records based on another set of data by eliminating the need for temporary tables or multiple queries.
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
When to Use ON UPDATE CASCADE in SQLite
"ON UPDATE CASCADE" is an important feature in SQLite that simplifies data management and ensures data integrity by automatically propagating changes from a parent table to its related child tables. This feature plays a crucial role in maintaining consistency and reducing the need for manual updates
4 min read
How to Use the IN Operator with a SubQuery in SQL?
In this article, we will see the use of IN Operator with a SubQuery in SQL. IN operator is used to compare the column values with the list of values. The list of values is defined after IN query in SQL. If we don't know the exact list of values to be compared, we can generate the list of values usin
2 min read
When to Use ON UPDATE CASCADE in PL/SQL?
In Oracle PL/SQL, managing the update of related records in child tables can be challenging, especially when dealing with complex data relationships. The ON UPDATE CASCADE option in Oracle provides a powerful solution to automate the update of child records whenever the corresponding parent record i
4 min read
Having vs Where Clause in SQL
In SQL, filtering data is important for extracting meaningful insights from large datasets. While both the WHERE and HAVING clauses allow us to filter data, they serve distinct purposes and operate at different stages of the query execution process. Understanding the difference between these clauses
4 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
INNER JOIN ON vs WHERE clause in SQL Server
In SQL Server, joining tables and filtering data are essential for retrieving meaningful information. The INNER JOIN operation is used to combine rows from multiple tables based on a matching condition, while the WHERE clause allows for further filtering of results. In this article, we will PL/SQL S
7 min read
Where clause in MS SQL Server
In this article, where clause will be discussed alongside example. Introduction : To extract the data at times, we need a particular conditions to satisfy. 'where' is a clause used to write the condition in the query. Syntax : select select_list from table_name where condition A example is given bel
1 min read
INNER JOIN ON vs WHERE clause in MySQL
When working with MySQL queries that involve multiple tables, understanding how to effectively use INNER JOIN ON versus the WHERE clause can significantly impact query performance and clarity. These two SQL constructs serve distinct purposes in combining data from different tables based on specific
5 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
When to Use "ON UPDATE CASCADE" in MySQL
In MySQL, the ON UPDATE CASCADE option in foreign key constraints indicates that anytime an update is made to a row in the parent table, matching rows in child tables are correspondingly updated. This feature provides assurance for referential integrity and consistency of related data in a related s
7 min read