0% found this document useful (0 votes)
32 views

Lecture 5db

Set operators allow combining the results of multiple queries. The main set operators are: 1) UNION combines results and removes duplicates. UNION ALL includes all duplicates. 2) INTERSECT returns rows that are selected by both queries. 3) MINUS returns rows selected by the first query that are not in the second query. Queries using set operators are called compound queries. Examples demonstrate combining results from different tables using UNION, UNION ALL, INTERSECT and MINUS.

Uploaded by

mohsin dish
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Lecture 5db

Set operators allow combining the results of multiple queries. The main set operators are: 1) UNION combines results and removes duplicates. UNION ALL includes all duplicates. 2) INTERSECT returns rows that are selected by both queries. 3) MINUS returns rows selected by the first query that are not in the second query. Queries using set operators are called compound queries. Examples demonstrate combining results from different tables using UNION, UNION ALL, INTERSECT and MINUS.

Uploaded by

mohsin dish
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture 11

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

101 ANNA 101 HARIL

102 CATRIN 102 JACK

103 103 VINTA


VINTA
Example
SELECT f_name FROM employe_norway
UNION
SELECT f_name FROM employe_usa;
EMPLOYE_DETAILS
F_NAME
ANNA
CATRIN
VINTA
HARIL
JACK
Union ALL
• All rows selected by either query, including all
duplicates.
Syntax:
select column list from table1
UNION ALL
select column list from table2
Example
SELECT f_name FROM employe_norway
UNION ALL
SELECT f_name FROM employe_usa
F_NAME

ANNA

CATRIN

VINTA

HARIL

JACK

VINTA
Example
AUTHOUR

CITY STATE ZIP_CODE

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

EMPLOYE NORWAY EMPLOYE USA

E_ID F_NAME E_ID F_NAME

101 HARIL
101 ANNA

102 JACK
102 CATRIN

VINTA 103 VINTA


103
Example
SELECT f_name FROM employe_norway
INTERSECT
SELECT f_name FROM employe_usa;
F_NAME

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

SELECT empno FROM emp


Minus
SELECT deptno FROM dept
order by empno

You might also like