Lecture 5db
Lecture 5db
Set operators
Set operators
• Set operators combine the results of two
component queries into a single result.
• Queries containing set operators are called
compound queries.
Types of set operator
• Union
• Union all
• Intersect
• Minus
Union
• The UNION query allows you to combine the result
sets of 2 or more "select" queries.
• It removes duplicate rows between the various
"select" statements.
Syntax:
select column list…
from table1
UNION
select column list
from table2
Example
EMPLOYE NORWAY:
EMPLOYE USA:
E_ID F_NAME E_ID F_NAME
ANNA
CATRIN
VINTA
HARIL
JACK
VINTA
Example
AUTHOUR
Nashville TN 37215
Lawrence KS 66044
Corvallis OR 97330
Example
SELECT City, State, Zip_c0de FROM Authors WHERE
State IN ('KS', 'TN')
UNION
SELECT City, State, Zip_code FROM Authors WHERE
State IN ('OR' 'TN');
CITY STATE ZIP_CODE
Nashville TN 37215
Lawrence KS 66044
Corvallis OR 97330
SELECT City, State, Zip_code FROM Authors
WHERE State IN ('KS', 'TN')
UNION ALL
SELECT City, State, Zip _code FROM Authors
WHERE State IN ('OR' 'TN');
CITY STATE ZIP_CODE
Nashville TN 37215
Lawrence KS 66044
Corvallis OR 97330
Nashville TN 37215
INTERSECT
• The INTERSECT query allows to return the results of 2 or
more "select" queries.
• It only returns the rows selected by all queries. If a
record exists in one query and not in the other, it will be
omitted from the INTERSECT results.
• In short, it returns distinct rows selected by both queries
Syntax:
select column list from table1
INTERSECT
select column list from table2;
Example
101 HARIL
101 ANNA
102 JACK
102 CATRIN
VINTA
Minus
• All distinct rows selected by the first query but
not the second.
• Syntax :
select column list from table1
Minus
select column list from table2
Example
SELECT f_name FROM employe_norway
Minus
SELECT f_name FROM employe_usa
F_NAME
ANNA
CATRIN
Using Order by clause in set operator