You are on page 1of 3

EXPERIMENT-9

AIM – To Implement Various Set Operators On The Tables.

SQL SET OPERATORS SQL set operators allows combine results from two or more
SELECT statements. There are situations when we need to combine the results from two or more
SELECT statements. SQL enables us to handle these requirements by using set operations. The
result of each SELECT statement can be treated as a set, and SQL set operations can be applied
on those sets to arrive at a final result. At first sight this looks similar to SQL joins although there
is big difference. SQL joins tends to combine columns i.e. with each additionally joined table it
is possible to select more and more columns. SQL set operators on the other hand combine rows
from different queries with strong preconditions - all involved SELECTS must:

 retrieve the same number of columns and


 the data types of corresponding columns in each involved SELECT must be compatible
(either the same or with possibility implicitly convert to the data types of the first
SELECT statement).

SYNTAX select col_1, col_2, col_3, ... col_n from table_1 set operator
select col_1, col_2, col_3, ... col_n from table_2 set operator
...
...
select col_1, col_2, col_3, ... col_n from table_n;

UNION OPERATOR Multiple queries can be combined by forming a union of them. The
SQL UNION operator allows manipulation of results returned by two or more queries by
combining the results returned by two or more queries by combining the results of each query
into a single result set. By default, the UNION operator removes duplicate rows from the result.

SYNTAXselect <columnname1> from <tablename1>


UNION
select <columnname2> from <tablename2>

For Example
UNION ALL OPERATOR The UNION ALL operator is similar to UNION operator i.e.
allows manipulation of results returned by two or more queries by combining the results returned
by two or more queries by combining the results of each query into a single result set except only
one difference that it does not remove duplicate rows from the result. If ALL option is used, all
rows including duplicates , are included in the results.

SYNTAX select <columnname1> from <tablename1>


UNION ALL
select <columnname2> from <tablename2>

For Example

INTERSECT OPERATOR The INTERSECT(set intersection) operator outputs only those


rows that are common in multiple tables. To carry out INTERSECT operator, the participating
table must be unique compatible i.e., they must produce same number and same type of attributes
in the result.

SYNTAX select <columnname1> from <tablename1>


INTERSECT
select <columnname2> from <tablename2>
For Example

MINUS OPERATORThe MINUS operator (set difference) operator returns rows unique to
the first query i.e., all those rows that are selected by first query, but which are not in the
following query, are returned. In a simple way, we can define it as it firsts selects the rows
according to the first query and then removes the rows selected by the second query from it.

SYNTAX select <columnname1> from <tablename1>


MINUS
select <columnname2> from <tablename2>

For Example

You might also like