You are on page 1of 4

Set operator

set operators to compare rows from two or more tables or to combine


the results obtained from two or more queries to obtain the final result. These
operators are used to join the results of two (or more) SELECT statements
There are certain rules which must be followed to perform operations using
SET operators in SQL. Rules are as follows:
The number and order of columns must be the same.
Data types must be compatible.
TYPES OF SET OPERATOR
1. Union
2. Union all
3. Intersect
4. Minus
1. UNION:
o UNION will be used to combine the result of two select statements.
o Duplicate rows will be eliminated from the results obtained after performing the
UNION operation.

EXAMPLE

select * from emp1 union select * from emp2 If select id from emp1 union select id from emp2;
2. UNION ALL

 This operator combines all the records from both the queries.
 Duplicate rows will be not be eliminated from the results obtained after
performing the UNION ALL operation.
Example
select * from emp1 union all select * from emp2;
(A U B)
3. INTERSECT:
o It is used to combine two SELECT statements, but it only returns the records which
are common from both SELECT statements.

select id from emp1 intersect select * from emp1 intersect


select id from emp2; select * from emp2;
A∩B

4. MINUS

o It displays the rows which are present in the first query but absent in the
second query with no duplicates.

select * from emp1 minus select * from emp2 minus


select * from emp2;(A-B) select * from emp1;(B-
A)

You might also like