0% found this document useful (0 votes)
20 views11 pages

11.SQL Queries-Set Operators

The document explains SQL set operators used to combine results from multiple SELECT statements, specifically focusing on UNION, UNION ALL, INTERSECT, and MINUS. It details the requirements for using these operators, such as the need for compatible data types and the ordering of results. Additionally, it emphasizes that the ORDER BY clause can only be applied once at the end of the combined query.

Uploaded by

dharani thaneeru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

11.SQL Queries-Set Operators

The document explains SQL set operators used to combine results from multiple SELECT statements, specifically focusing on UNION, UNION ALL, INTERSECT, and MINUS. It details the requirements for using these operators, such as the need for compatible data types and the ordering of results. Additionally, it emphasizes that the ORDER BY clause can only be applied once at the end of the combined query.

Uploaded by

dharani thaneeru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL QUERIES-7

SET Operators :
• Set operators are used to join the results of two (or
more) SELECT statements.
• The SET operators available in Oracle are
UNION,UNION ALL,INTERSECT,and MINUS.
• Same number of columns must be selected by all
participating SELECT statements.Column names used in
the display are taken from the first query.
• Data types of the column list must be
compatible/implicitly convertible by oracle. Oracle will
not perform implicit type conversion if corresponding
columns in the component queries belong to different
data type groups.
• Positional ordering must be used to sort the result
set. Individual result set ordering is not allowed
with Set operators. ORDER BY can appear once at
the end of the query.
• UNION and INTERSECT operators are commutative,
i.e. the order of queries is not important; it doesn't
change the final result.
• Set operators can be the part of sub queries.
• Consider tables:

1.depositor //table name


• custname(a,b,c,d)
• balance(10000,15000,13000,14000)

2.Borrower //table name


• custname(e,a,f,b)
• amount(20000,25000,15000,10000)
1.UNION OPERATOR

• When multiple SELECT queries are joined using


UNION operator, Oracle displays the combined
result from all the compounded SELECT
queries,after removing all duplicates and in sorted
order (ascending by default), without ignoring the
NULL values.
• syntax-select statement1 UNION select statement2;
• R1 is the relation returned by "select statement1"
and R2 is returned by "select statement2".
• UNION of R1 and R2 =R3.
• Here R3 contains the rows that are either in R1 or
in R2 or in both.
• UNION eliminates the duplicate values.
• e.g. show all the customers who have either an
account or a loan or both.
• select custname from depositor UNION select
custname from borrower;
2.UNION ALL

• UNION and UNION ALL are similar in their


functioning with a slight difference. But UNION ALL
gives the result set without removing duplication
and sorting the data.
• select custname from depositor UNION ALL select
custname from borrower;
• It will not remove duplicate values
3.INTERSECT
• Using INTERSECT operator, Oracle displays the
common rows from both the SELECT statements,
with no duplicates and data arranged in sorted
order (ascending by default).
• It returns a relation that contains the common rows
of 2 relations returned by select statement1 and
select statement2.
• syntax-select statement1 INTERSECT select
statement2;
• select custname from depositor INTERSECT select
custname from borrower;
4. MINUS
• Minus operator displays the rows which are present
in the first query but absent in the second query,
with no duplicates and data arranged in ascending
order by default
• It returns a relation which contains rows of R1
which are not in R2.
• syntax-select statement1 MINUS select statement2;
• select custname from depositor MINUS select
custname from borrower;
Using ORDER BY clause in SET
operations
• The ORDER BY clause can appear only once at the end
of the query containing compound SELECT statements.
• It implies that individual SELECT statements cannot
have ORDER BY clause.
• Additionally, the sorting can be based on the columns
which appear in the first SELECT query only.
• For this reason, it is recommended to sort the
compound query using column positions.
• Example:
• SELECT employee_id, first_name, salary FROM
employees WHERE department_id=10 UNION
SELECT employee_id, first_name, salary FROM
employees WHERE department_id=20 ORDER BY 3;

You might also like