EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Oracle Tutorial Oracle EXISTS
 

Oracle EXISTS

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

Oracle EXISTS

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Oracle EXISTS 1

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:

Oracle EXISTS 2

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:

Oracle EXISTS 3

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:

UPDATE STATEMENT

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:

Delete Statement

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 –

  1. Oracle Date Functions
  2. Oracle Temporary Table
  3. Oracle EXTRACT()
  4. Oracle While Loop
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW