Relational Algebra Operations From Set Theory
Relational Algebra Operations From Set Theory
Introduction
Relational Algebra is a fundamental query language used in
databases. It provides a set of operations to manipulate and
retrieve data stored in relational databases. Some of these
operations are derived from set theory, allowing us to
perform operations like union, intersection, difference, and
Cartesian product on relations (tables).
Set-Theoretic Operations in Relational Algebra
1. Union ( ∪ )
Definition:
The union of two relations R and S (denoted as R ∪ S)
includes all tuples that appear in either R or S or both.
It removes duplicate tuples since relations are sets (not
multisets).
Conditions:
Both relations must have the same number of
attributes.
The corresponding attributes must have the same
domain (same data types).
Example:
Let R(A, B) and S(A, B) be two relations:
A B
1 2
4
3
A B
3 4
5 6
A B
1 2
3 4
5 6
2. Intersection ( ∩ )
Definition:
The intersection of two relations R and S (denoted as
R ∩ S) returns only those tuples that are present in both
relations.
Conditions:
Same as Union (same number of attributes and
domains).
Example:
Intersection Result (R ∩ S):
A B
3 4
3. Set Difference ( − )
Definition:
The difference of two relations R and S (denoted as R -
S) returns tuples that are in R but not in S.
Example:
Difference Result (R - S):
A B
1 2
A B
5 6
4. Cartesian Product ( × )
Definition:
The Cartesian Product of two relations R and S
(denoted as R × S) combines every tuple of R with
every tuple of S.
The resulting relation contains all possible pairwise
combinations of tuples.
Unlike union, intersection, and difference, the
relations do not need to have the same schema.
Example:
Let R(A, B) and S(C, D) be two relations:
A B
1 2
3 4
C D
5 6
A B C D
1 2 5 6
3 4 5 6
1. JOIN Operation
The JOIN operation is used to combine tuples from two
relations based on a common attribute. It is one of the
most frequently used operations in databases.
Example of Natural Join
Given relations:
2. DIVISION Operation
The DIVISION operation is used in queries involving
"all" conditions, such as "Find students who have
completed all required courses." It is useful for finding
tuples in one relation that are associated with all tuples in
another relation.
Definition
If relation R(A, B) is divided by relation S(B), the result
is a relation T(A) containing values of A that are
associated with all values of B in S.
Example
Given relations:
Student
Alice
Bob
(Explanation: Alice and Bob have taken both Math and
Science, while Carol is missing Science, so she is not
included.)