Set Operator Notes
Set Operator Notes
Special operators which combines the result of two queries into a single result.
SQL consists of Set operators like
Union
Union All
Intersect
Minus
Set Operators – Union
Used to combine the results of two select statements.
A Union combines the results of two or more quires into one table.
It eliminates duplicate rows from its result.
In case of Union, number of columns and data type must be same in both the tables.
Rules:-
The number of column in each SELECT statement must match.
The data types in the columns should be same.
The columns in each select statement must be in the same order.
Must have the same expressions and aggregate function in each SELECT statement.
The UNION operator selects only distinct values by default and it doesn’t select
duplicate values.
Union Syntax & Example
Syntax
Select * from table 1
Union
Select * from table2;
Example
Select name from emp
Union
Select name from cust;
Union All
This operator combines all records from both the queries.
It does not avoid the duplicate entries.
Syntax
Select * from table 1
Union all
Select * from table2;
Example
Select name from emp
Union
Select name from cust;
Intersect
It is used to select common values in a column from tables.
It is used to combine two SELECT statements, but it only returns the records which
are common from both statements.
Syntax
Select * from table 1
intersect
Select * from table2;
Example
Select name from emp
intersect
Select name from cust;
Minus
It displays the rows which are present in the first query but absent in the second
query with no duplicated.
It is used to select value which is present in one table but not in another.
Syntax
Select * from table 1
minus
Select * from table2;
Example
Select name from emp
minus
Select name from cust;