EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle EXISTS
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Oracle EXISTS

By Priya PedamkarPriya Pedamkar

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.

Syntax:

Given below is the syntax of Oracle EXISTS:

SELECT
*
FROM
table
WHERE
EXISTS(subquery);

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)

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
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more