Updated March 4, 2023
Introduction to Oracle EXISTS
EXISTS is a type of condition in Oracle database which can be defined as a condition or operator that is used with a sub query ( inner query or nested query is a query within another SQL query ) and upon execution of the sub query, if the sub query returns at least one row then the condition is considered to be met and hence the condition or operator is often used with sub query to check the existence of rows.
Syntax:
Given below is the syntax of Oracle EXISTS:
SELECT
*
FROM
table
WHERE
EXISTS(subquery);
Parameters:
- table: It refers to the name of the table.
- subquery: It refers to the sub-query which is a SELECT statement and it is query within another query.
How EXISTS work in Oracle?
- This function is used with a sub query to evaluate whether any row is returned by the sub-query.
- So when we execute the query the sub-query is executed first and in case the sub query returns any row or value then the EXISTS condition is met and it is evaluated to be true whereas in case no value or record is returned by the condition then the EXISTS condition is not met it evaluates to be FALSE.
- The execution of Oracle EXISTS condition is not at all efficient because the sub-query is re run every time for every row which results in loss of efficiency.
Examples of Oracle EXISTS
Given below are the examples mentioned:
It can be used with both DQL and DML statements in Oracle which means we can use it with SELECT, INSERT, UPDATE and DELETE statements.
Example #1
EXISTS WITH SELECT STATEMENT.
In this case, we are going to see how we can use EXISTS with SELECT statement with the help of example. In this example we will try to get the details of both employee and vehicle from the employee table and vehicle table for all those employees who are present in both vehicle and employee table. We will use EXISTS condition for this.
Code:
select * from employee where EXISTS(
select * from vehicle where
employee.EMPLOYEE_ID = vehicle.EMPLOYEE_ID);
In the above query the ORACLE EXISTS condition will return all the rows from the vehicle table where the employee id in the vehicle table is equal to the employee id in the employee table.
Output:
As we can see in the screen shot the query returns the above records.
Example #2
SELECT STATEMENT WITH NOT EXISTS.
In this case, we are going to see how we can use EXISTS with SELECT statement with the help of example. In this example we will try to get the details of both employee and vehicle from the employee table and vehicle table for all those employees who are not present in both vehicle and employee table (meaning not common employees). We will use NOT EXISTS condition for the same.
Code:
select * from employee where NOT EXISTS(
select * from vehicle where
employee.EMPLOYEE_ID = vehicle.EMPLOYEE_ID);
In the above query the ORACLE NOT EXISTS condition will return all the rows from the vehicle table where the employee id in the vehicle table is not equal to the employee id in the employee table.
Output:
As we can see in the screen shot the query returns the above records.
Example #3
EXISTS WITH INSERT STATEMENT.
In this case we are going to INSERT records into a table using the ORACLE EXISTS. In this example we are going to insert the customer id, customer name and place in customers table with the employee id, employee name and employee city from the employee table for the condition where the employee id present in the employee table is equal to the employee id present in the vehicle table.
Code:
INSERT INTO customers(CUSTOMER_ID,CUSTOMER_NAME,PLACE)
select employee.EMPLOYEE_ID, employee.NAME,employee.CITY
from employee where EXISTS(
select * from vehicle where
employee.EMPLOYEE_ID = vehicle.EMPLOYEE_ID);
Output:
As we can see in the above screen shot the five records satisfied the condition, so five records got inserted in the CUSTOMERS table.
Example #4
EXISTS WITH UPDATE STATEMENT.
In this example we will use EXISTS with UPDATE statement. In this example we will update the name column of customer table with the names of the employee table using the EXISTS operator.
Code:
UPDATE customers
SET customer_name =(SELECT employee.name
FROM employee
WHERE employee.EMPLOYEE_ID = customers.CUSTOMER_ID)
WHERE EXISTS (SELECT employee.name
FROM employee
WHERE employee.EMPLOYEE_ID = customers.CUSTOMER_ID);
Output:
As we can see in the above screen shot the five records satisfied the condition, so five records got updated in the CUSTOMERS table.
Example #5
EXISTS WITH DELETE STATEMENT.
In this example we are going to EXISTS with DELETE statement. In this example we are going to delete the records from CUSTOMERS table where the employee id from the employee table is equal to customer id from the customer table using the EXISTS condition.
Code:
DELETE FROM customers
WHERE EXISTS (SELECT *
FROM employee
WHERE customers.CUSTOMER_ID = employee.EMPLOYEE_ID);
Output:
As we can see in the above screen shot the five records satisfied the condition, so five records got deleted in the CUSTOMERS table.
Recommended Articles
This is a guide to Oracle EXISTS. Here we discuss how EXISTS work in oracle with query examples for better understanding. You may also have a look at the following articles to learn more –