This Oracle tutorial explains how to use the Oracle EXISTS condition with syntax and examples.
The Oracle EXISTS condition is used in combination with a subquery and is considered "to be met" if the subquery returns at least one row. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the EXISTS condition in Oracle/PLSQL is:
The subquery is a SELECT statement. If the subquery returns at least one record in its result set, the EXISTS clause will evaluate to true and the EXISTS condition will be met. If the subquery does not return any records, the EXISTS clause will evaluate to false and the EXISTS condition will not be met.
Let's look at a simple example.
The following is a SELECT statement that uses the EXISTS condition:
This Oracle EXISTS condition example will return all records from the customers table where there is at least one record in the order_details table with the matching customer_id.
The Oracle EXISTS condition can also be combined with the NOT operator.
For example,
This Oracle EXISTS example will return all records from the customers table where there are no records in the order_details table for the given customer_id.
The following is an example of an INSERT statement that uses the EXISTS condition:
The following is an example of an UPDATE statement that uses the EXISTS condition:
The following is an example of a DELETE statement that uses the EXISTS condition: