You are on page 1of 5

Practical No.

Aim: To implement basic set operations in SQL

Theory:
Basically, a table could be defined as a relation, i.e. a set of elements. Thus in
relational database all the data is stored in the form of sets. A query returns a set of
rows, so you can do additional set operations on the output of a query.

Basic Set Operations

The three basic set operations are:


❑ Union
❑ Intersect
❑ Minus

Union Operation

The UNION operator is used to combine the result-set of two or more SELECT
statements.

Syntax:

SELECT column_name(s) FROM table1


UNION
SELECT column_name(s) FROM table2;

Notice that each SELECT statement within the UNION must have the same number
of columns. The columns must also have similar data types. Also, the columns in each
SELECT statement must be in the same order.

Consider the following tables:

Department

LOCID DEPTNAME WAREHOUSENAME


---------- ---------- ----------
1400 it shimla
1500 shipping raipur
1600 accounting jabalpur
1400 benefits southlake

Warehouseinfo

LOCID WAREHOUSENAME
---------- ----------
1400 southlake
3452 raipur
2345 bihar

Page 1 of 5
Example:-
SQL> select locid, warehousename from department
UNION
select locid, warehousename from warehouseinfo;

Result:- LOCID WAREHOUSENAME


---------- ----------
1400 shimla
1400 southlake
1500 raipur
1600 jabalpur
2345 bihar
3452 raipur

Union All Operation

The UNION operator selects only distinct values by default. To allow duplicate
values, use the ALL keyword with UNION.

Syntax

SELECT column_name(s) FROM table1


UNION ALL
SELECT column_name(s) FROM table2;

Note: The column names in the result-set of a UNION are usually equal to the
column names in the first SELECT statement in the UNION.

Example:

SQL> select locid, warehousename from department


UNION ALL
select locid, warehousename from warehouseinfo;

Result: - LOCID WAREHOUSENAME


---------- ----------
1400 shimla
1500 raipur
1600 jabalpur
1400 southlake
1400 southlake
3452 raipur
2345 bihar

Intersect Operation

Page 2 of 5
The SQL INTERSECT operator is used to return the results of 2 or more SELECT
statements. However, 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.
Each SQL statement within the SQL INTERSECT must have the same number of
fields in the result sets with similar data types.
Syntax

SELECT column_name(s) FROM table1


INTERSECT
SELECT column_name(s) FROM table2;

Example:

SQL> select locid from department INTERSECT select locid from warehouseinfo;

Result: - LOCID
----------
1400

MINUS Operation:

The SQL MINUS 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 MINUS query must have the same number of
fields in the result sets with similar data types.

Syntax

SELECT column_name(s) FROM table1


MINUS
SELECT column_name(s) FROM table2;

Example:-
SQL> select locid, warehousename from department MINUS select locid,
warehousename from warehouseinfo;

Result: - LOCID WAREHOUSENAME


---------- ----------
1400 shimla
1500 raipur
1600 jabalpur

Page 3 of 5
Lab Exercise for Practical No. 4

1. List all the customers who are depositors but not borrowers

2. List all the customers who are both depositors and borrowers

3. List all the customers, along with their amount, who are either borrowers or
depositors and living in city NAGPUR

Ans: D1.CNAME, D1.AMOUNT FROM DEPOSIT D1, CUSTOMER Cl


WHERE C1.CITY = 'NAGPUR' AND D1.CNAME = C1.CNAME UNION
ALL SELECT BR1.CNAME, BR1.AMOUNT FROM BORROW BR1,
CUSTOMER C2 WHERE C2.CITY = 'NAGPUR' AND BR1.CNAME =
C2.CNAME;

Page 4 of 5
Page 5 of 5

You might also like