How To Use Set Operators In Oracle
Description
There are three types of set operator in Oracle are-
• UNION
• INTERSECT• MINUS
Set Operators combine two or more queries into one result. Suppose, we want the following details from employee table.
• List of all the different designations in Dept_id 101 and 107.
• List the jobs common to dept_id 101 and 107.• List the jobs unique to dept_id 101.
Union
It helps in retrieval of rows of first query plus rows of second query, less duplicate rows.
Syntax
- Select <statement 1>
- UNION
- Select <statement 2>
- [Order-by-clause]
Example
- Select Emp_job from Employee
- Where Dept_no = 101
- UNION
- Select Emp_job from Employee
- Where Dept_no = 104;
Points to be kept in mind while using UNION operator
- Two select statements may not contain an ORDER BY CLAUSE, however, the final result of the entire UNION operation can be ordered.
- The number of columns retrieved by the first, select must be equal to the number of columns retrieved by second select.
- The data types of columns retrieved by the select statements should be same.
Example
- Select Emp_id, Emp_name
- From employee
- Where Dept_no = 101
- UNION
- Select Emp_Id, Ename
- from employee
- Where dept_no = 104;
Intersect Operator
Description
Syntax
- Select <statement 1>
- INTERSECT
- Select <Statement 2>
- [order-by-clause]
- Select Emp_job
- From employee
- Where Dept_no = 107
- INTERSECT
- Select Emp_job
- from employee
- Where Dept_no = 106;
Minus Operator
Description
Minus operator returns the rows unique to first query.
Syntax
- Select <statement 1>
- MMINUS
- Select <statement 2>
- [Order-by-clause]
Example
- Select Emp_job from employee
- where dept_no = 101
- Minus
- Select Emp_job from employee
- where dept_no=102;
Summary
Thus, we learned, Set operators are used to combine the information of similar type from one or more than one table and we learn, how to use these operators with examples.