Updating Rows Based on Subquery Result

In this approach, we’ll utilize a subquery within the UPDATE statement to update table rows based on specific conditions.

Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition IN (subquery);

Example: Increasing Salaries for Sales Department Employees

Suppose we want to increase the salaries of employees in the Sales department by 10%. So, in this example we will use subquery in the condition of query. In the subquery, we selected deptid from departments belonging to Sales department.

UPDATE employees 
SET salary = salary * 1.1
WHERE deptid = (SELECT deptid FROM departments WHERE deptname = 'Sales');

Output:

Updating Rows Based on a Subquery Result

Explanation: In this example, the query updates the salary column for employees in the Sales department. It multiplies their current salaries by 1.1, effectively increasing them by 10%.

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 in PL/SQL using subqueries, providing insights into syntax, examples, and practical applications, contributing to better data handling and application development.

Similar Reads

Setting up Environment

First, let’s create a table named employees with columns employee_id, salary, and department_id....

How to Update Table Rows in PL/SQL Using Subquery

Where we use more than one select statement, it is called Subquery. Updating table rows in PL/SQL using subqueries involves using a subquery within the UPDATE statement to specify which rows to update and how they should be updated. The subquery acts as a filter, allowing you to selectively update rows based on certain conditions. It is also known as inner querry....

1. Updating Rows Based on Subquery Result

In this approach, we’ll utilize a subquery within the UPDATE statement to update table rows based on specific conditions....

2. Updating Rows Based on Subquery Result

In this scenario, we’ll demonstrate updating table rows based on multiple conditions specified in a subquery....

3. Updating Rows Based on Subquery Result (Using Aggregate Function)

Here, we’ll showcase updating table rows based on aggregated subquery results, providing a more complex yet powerful data modification technique....

Conclusion

Updating table rows in PL/SQL using subqueries is a flexible way to modify data based on specific conditions. By utilizing subqueries within the UPDATE statement, you can efficiently update rows in accordance with various requirements, ultimately contributing to better data management and application development....