Professional Documents
Culture Documents
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:
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.
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.
For Example
MINUS OPERATORThe 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.
For Example