How To Use Exists Condition In OraclePLSQL
Description
Exists condition is used in query and the query result depends on the row, which exists in a table. The sub query returns at least one row. It can be used in a select, insert, update or delete command.
Syntax
- Select *
- from Table_name
- where Exists (Subquery) ;
Example
The example, given below, uses select statement.
- Select *
- from Employee
- where Exists
- (
- select * from Departments
- where Employee.Dept_no = Department.Dept_id
- );
Example
The example, given below, uses select statement with Not Exists.
- Select *
- from Employee
- where Not Exists
- (
- select * from Departments
- where Employee.Dept_no = Department.Dept_id
- );
Example
The example, given below, uses insert statement.
- insert into Supplier(supplier_id, supplier_name)
- select Dept_no, Emp_name
- from Employee
- where exists
- (
- select * from Department
- where Employee.Dept_no = department.Dept_id
- );
Example
The example, given below, uses Update Statement.
- update CUSTOMER
- set CUSTOMER_NAME = (Select Dept_name
- from Department
- where Department.dept_id = CUSTOMER.CUSTOMER_ID)
- where Exists
- (select Dept_name
- from Department
- where Department.dept_id = CUSTOMER.CUSTOMER_ID
- );
Example
The example, given below, uses Delete statement.
- Delete from Customer
- where Exists
- (
- select * from Department
- where Customer.Customer_id = Department.Dept_id
- );
Summary
Thus, we learnt, Exists condition is used in query and the query result depends on the row, which exists in a table. We learnt, how to use this condition in Oracle with the examples.