### Set Operations : Union, Set Difference, Set Intersection

The union Operation performs binary union between two given relations. It is used when we need some attributes that appear in either or both of the two relations. It is denoted as U.

Notation − r U s

Where r and s are either database relations or relation result set (temporary relation). For a union operation r U s to be valid, the following conditions must

1. The relation r and s must have the same number of attributes.
2. The domains of the ith attribute of r and the ith attribute of s must be the same for all i.
3. Duplicate tuples are automatically eliminated.

For example consider the two table Depositor and borrower

now to list all the customers from both table, the query will be

The relation that results from the preceding query is will be :

SET DIFFERENCE :

The set-difference operation, denoted by −, allows us to find tuples that are in one relation but are not in another. The expression r − s produces a relation containing those tuples in r but not in s. For example at the Depositor and Borrower, to list all the customers from Depositor table who did not borrow from bank, the query will be :

The relation that results from the preceding query is will be :

As with the union operation, we must ensure that set differences are taken between compatible relations. Therefore, for a set difference operation r − s to be valid, we require that the relations r and s be number of attributes, and that the domains of the ith attribute of r and the ith attribute of s be the same.

Set Intersection :

The set-intersection operation finds tuples in both the relations which are common to them. In other words it picks only duplicate tuples from the relations. It is denoted as ∩. For example consider the relations Depositor and Borrower :

Now to lists all the customers who Deposit and also Borrow from bank, the query will be

The relation that results from the preceding query is will be :

Next Topic :-