Relational Algebra
Relational Algebra
Relational Algebra
Relational Algebra
2
Relational Algebra
3
Relational Algebra Operations
4
Relational Algebra Operations
5
Instance of DreamHome rental
database
6
Selection
• predicate (R)
– Works on a single relation R and defines a
relation that contains only those tuples (rows) of
R that satisfy the specified condition (predicate).
7
Selection
8
Example - Selection
9
Relational Algebra Operations
10
© Pearson Education Limited 1995, 2005
Projection
• col1, . . . , coln(R)
– Works on a single relation R and defines a
relation that contains a vertical subset of R,
extracting the values of specified attributes and
eliminating duplicates.
11
© Pearson Education Limited 1995, 2005
Projection
12
© Pearson Education Limited 1995, 2005
Example - Projection
13
Relational Algebra Operations
14
Union
• RS
– Union of two relations R and S defines a relation
that contains all the tuples of R, or S, or both R
and S, duplicate tuples being eliminated.
– R and S must be union-compatible.
15
© Pearson Education Limited 1995, 2005
Union compatible
16
Example - Union
17
© Pearson Education Limited 1995, 2005
Example - Union
18
© Pearson Education Limited 1995, 2005
Relational Algebra Operations
19
© Pearson Education Limited 1995, 2005
Set Difference
• R–S
– Defines a relation consisting of the tuples that
are in relation R, but not in S.
– R and S must be union-compatible.
20
© Pearson Education Limited 1995, 2005
Example - Set Difference
• List all cities where there is a branch office
but no properties for rent.
city(Branch) – city(PropertyForRent)
21
© Pearson Education Limited 1995, 2005
Example - Set Difference
22
© Pearson Education Limited 1995, 2005
Relational Algebra Operations
23
© Pearson Education Limited 1995, 2005
Intersection
• RS
– Defines a relation consisting of the set of all
tuples that are in both R and S.
– R and S must be union-compatible.
24
© Pearson Education Limited 1995, 2005
Example - Intersection
• List all cities where there is both a branch
office and at least one property for rent.
city(Branch) city(PropertyForRent)
25
© Pearson Education Limited 1995, 2005
Example - Intersection
26
© Pearson Education Limited 1995, 2005
Relational Algebra Operations
27
© Pearson Education Limited 1995, 2005
Cartesian product
• RXS
– Defines a relation that is the concatenation of
every tuple of relation R with every tuple of
relation S.
28
© Pearson Education Limited 1995, 2005
Example - Cartesian product
• List the names and comments of all clients who
have viewed a property for rent.
(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment
(Viewing))
29
Example - Cartesian product
30
End….
31