The Oracle database management system provides a robust and efficient way to manage and manipulate data. One of the fundamental operations in any database system is the ability to update existing data. Oracle supports this operation through its UPDATE statement, which can be used in conjunction with a SELECT statement to update data based on specific conditions. In this article, we will explore the Oracle UPDATE statement with a SELECT statement, including its syntax, examples, and best practices.
Introduction to Oracle UPDATE Statement

The Oracle UPDATE statement is used to modify existing data in a table. The basic syntax of the UPDATE statement is as follows:
UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;
This statement updates the specified columns in the table with the given values, where the condition in the WHERE clause is met.
Using SELECT Statement with UPDATE
In many cases, you may want to update data in a table based on data from another table or based on a complex condition that involves subqueries. Oracle allows you to use a SELECT statement within the UPDATE statement to achieve this. The syntax is as follows:
UPDATE table_name SET column1 = (SELECT expression FROM table_name2 WHERE condition) WHERE condition;
This syntax allows you to update the specified column in the table with the value returned by the SELECT statement, where the condition in the WHERE clause is met.
Component | Description |
---|---|
table_name | The name of the table to be updated. |
column1 | The name of the column to be updated. |
expression | The value to be assigned to the column, which can be a constant, a column name, or a complex expression. |
table_name2 | The name of the table from which to retrieve the value. |
condition | The condition that must be met for the update to occur. |

Examples of Oracle UPDATE with SELECT Statement

Let’s consider a few examples to illustrate the use of the UPDATE statement with a SELECT statement in Oracle.
Example 1: Updating a Column Based on a Simple Condition
Suppose we have two tables, EMPLOYEES and DEPARTMENTS, and we want to update the DEPARTMENT_NAME in the EMPLOYEES table based on the DEPARTMENT_ID.
UPDATE employees SET department_name = (SELECT department_name FROM departments WHERE department_id = employees.department_id) WHERE department_id IN (SELECT department_id FROM departments);
This statement updates the DEPARTMENT_NAME in the EMPLOYEES table with the corresponding department name from the DEPARTMENTS table, where the DEPARTMENT_ID matches.
Example 2: Updating Multiple Columns Based on a Complex Condition
Suppose we have two tables, ORDERS and CUSTOMERS, and we want to update the CUSTOMER_NAME and ADDRESS in the ORDERS table based on the CUSTOMER_ID.
UPDATE orders SET customer_name = (SELECT customer_name FROM customers WHERE customer_id = orders.customer_id), address = (SELECT address FROM customers WHERE customer_id = orders.customer_id) WHERE customer_id IN (SELECT customer_id FROM customers);
This statement updates the CUSTOMER_NAME and ADDRESS in the ORDERS table with the corresponding values from the CUSTOMERS table, where the CUSTOMER_ID matches.
Key Points
- Use the UPDATE statement with a SELECT statement to update data in a table based on data from another table or a complex condition.
- Ensure that the subquery returns only one row to avoid errors.
- Use the WHERE clause to specify the condition for the update.
- Use the SET clause to specify the columns to be updated and the values to be assigned.
- Use the SELECT statement within the UPDATE statement to retrieve the values to be assigned to the columns.
Best Practices for Using Oracle UPDATE with SELECT Statement
When using the UPDATE statement with a SELECT statement in Oracle, follow these best practices:
Optimize Subqueries
Optimize subqueries to improve performance. Use indexes, rewrite subqueries as joins, and avoid using correlated subqueries whenever possible.
Use Meaningful Table Aliases
Use meaningful table aliases to improve readability and avoid confusion. Avoid using the same alias for multiple tables.
Test and Verify
Test and verify the UPDATE statement with a SELECT statement before executing it on a large dataset. Use a small sample dataset to test the statement and verify the results.
What is the purpose of the UPDATE statement with a SELECT statement in Oracle?
+The purpose of the UPDATE statement with a SELECT statement in Oracle is to update data in a table based on data from another table or a complex condition.
How do I ensure that the subquery returns only one row?
+To ensure that the subquery returns only one row, use a unique identifier in the WHERE clause, such as a primary key or a unique index.
What are the best practices for using the UPDATE statement with a SELECT statement in Oracle?
+The best practices for using the UPDATE statement with a SELECT statement in Oracle include optimizing subqueries, using meaningful table aliases, and testing and verifying the statement before executing it on a large dataset.
In conclusion, the Oracle UPDATE statement with a SELECT statement is a powerful tool for updating data in a table based on data from another table or a complex condition. By following the best practices outlined in this article, you can ensure that your UPDATE statements are efficient, effective, and accurate.
Related Terms:
- Oracle update select from example
- Oracle update with select subquery
- Oracle update with join
- Oracle UPDATE statement
- Oracle update from another table