You are on page 1of 7

DEPARTMENT OF ELECTRONICS ENGINEERING Experiment No: 7

Semester T.E. Semester VI – Electronics Engineering

Subject Database Management Systems Laboratory.

Lectures Professor In-charge Prof. Shrikant Velankar

Practicals Professor In-Charge Prof. Rakshak P. Sood

Laboratory number M512B

Student Name Khooshi Tembhurne

Roll Number 20103A0078

Grade Subject Teacher’s


Signature

Experiment No: 7

Experiment Title To study and practice Set Operations in SQL.

Resources / Hardware: Software:


Apparatus
PC Oracle/MySQL/PostgreSQL &
Required
Internet

Objectives
1) To study different types of set operations.
2) To use these commands for a given table and perform various set
operations like union, union all, intersect and minus.

16
Historical Profile
Set operators are used to join the results of two (or more) SELECT
statements. The SET operators available in PostgreSQL are UNION,
UNION ALL, INTERSECT and MINUS.
All the SET operators share the same degree of precedence among them.
Instead, during query execution, PostgreSQL starts evaluation from left to
right or from top to bottom. If explicitly

parentheses are used, then the order may differ as parentheses would be
given priority over dangling operators.

17
Theory 1.UNION operator
In SQL the UNION operator combines the results of two SQL queries into
a single table. The two queries must result in the same number of columns
and compatible data types in order to unite. Any duplicate records are
automatically removed unless UNION ALL is used.
UNION can be useful in data warehouse applications where tables aren't
perfectly normalized. A simple example would be a database having tables
sales2005 and sales2006 that have identical structures but are separated
because of performance considerations. A UNION query could combine
results from both tables.
Note that UNION does not guarantee the order of rows. Rows from the
second operand may appear before, after, or mixed with rows from the first
operand. In situations where a specific order is desired, order by must be
used.
Note that UNION ALL may be much faster than plain union.

Screenshot:

18
UNION ALL Screenshot:

2. INTERSECT operator

The SQL INTERSECT operator takes the results of two queries and
returns only rows that appear in both result sets. For purposes of
duplicate removal the INTERSECT operator does not distinguish
between nulls. The INTERSECT operator removes duplicate rows
from the final result set. The INTERSECT ALL operator does not
remove duplicate rows from the final result set. Screenshot:

19
20
3. EXCEPT OPERATOR
DESCRIPTION
The SQL EXCEPT operator is used to return all rows in the first SELECT
statement that are not returned in the second SELECT statement.
Each SELECT statement within the EXCEPT query must have the same
number of fields in the result sets with similar data types. SYNTAX
The syntax for the SQL EXCEPT operator is:

21
SELECT expression1, expression2, ... expression_n
FROM tables
EXCEPT
SELECT expression1, expression2, ...
expression_n FROM tables;

Screenshot:

This SQL EXCEPT example returns all supplier_id values that are in
the suppliers table and not in the orders table. What this means is that if
a supplier_id value existed in the suppliers table and also existed in the
orders table, the supplier_id value would not appear in this result set.

Conclusion
Thus, it is has been experimentally proved that it is possible to perform set
operations like union, union all, intersection and minus operators between
two tables which helps in the creation and modification of database.

Real Life Set operations are used in numerous programming languages,


Application spreadsheets, and relational algebra.

22

You might also like