SQL SET OPERATIONS
The SQL Set operation is used to combine the
two or more SQL SELECT statements.
Types of Set Operation
Union
Union All
Intersect
Minus
1. UNION OPERATION:
This is used to combine result set of two or
more select statement without returning any
duplicate record.
To use union operation, each select statement
must have-
1. The same numbers of column selected
2. The same ordering
Syntax: SELECT column_name FROM table1
UNION
create table bca1(rollnum int primary key,
f_name varchar(10),l_name varchar(10),address
varchar(20));
create table bca2(rollnum int primary key, f_name
varchar(10),l_name varchar(10),address
varchar(20));
1. select f_name,l_name from bca1 union select
f_name,l_name from bca2; // to perform
union on
selected
columns
2. select * from bca1 union select * from bca2; //
2. UNION ALL OPERATION:
This is used to combine result set of two or
more select statement with duplicate record.
Syntax: SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;
3. INTERSECT OPERATION
It is used to combine two SELECT statements.
The Intersect operation returns the common
rows from both the SELECT statements.
In the Intersect operation, the number of
datatype and columns must be the same.
Syntax: SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;
4. MINUS OPERATION:
It combines the result of two SELECT
statements. Minus operator is used to display
the rows which are present in the first query but
absent in the second query.
It has no duplicates and data arranged in
ascending order by default.
Syntax: SELECT column_name FROM table1
MINUS
SELECT column_name FROM table2;