Lecture 1.3
Lecture 1.3
Relational Algebra
Join Operations
Grouping
Relational Calculus
Introduction
• 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.
• 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.
Example - Set Difference
• 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.
• RXS
– Defines a relation that is the concatenation of
every tuple of relation R with every tuple of
relation S.
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))
Example - Cartesian product and Selection
• R FS
– Defines a relation that contains tuples
satisfying the predicate F from the Cartesian
product of R and S.
– The predicate F is of the form R.ai S.bi where
may be one of the comparison operators (<,
, >, , =, ).
Theta join (-join)
R FS = F(R S)
• R S
– An Equijoin of the two relations R and S over all
common attributes x. One occurrence of each
common attribute is eliminated from the result.
Example - Natural join
• R S
– (Left) outer join is join in which tuples from R
that do not have matching values in common
columns of S are also included in result
relation.
29
Example - Left Outer join
• Produce a status report on property
viewings.
propertyNo, street, city(PropertyForRent)
Viewing
Semijoin
• R F S
– Defines a relation that contains the tuples of R that
participate in the join of R with S.
R F S = A(R F S)
Example - Semijoin
• RS
– Defines a relation over the attributes C that
consists of set of tuples from R that match
combination of every tuple in S.
(clientNo, propertyNo(Viewing))
(propertyNo(rooms = 3 (PropertyForRent)))
Aggregate Operations
• AL(R)
– Applies aggregate function list, AL, to R to define
a relation over the aggregate list.
– AL contains one or more (<aggregate_function>,
<attribute>) pairs .
• Main aggregate functions are: COUNT, SUM,
AVG, MIN, and MAX.
Example – Aggregate Operations
48
Example - Tuple Relational
Calculus
• List the names of clients who have viewed a
property for rent in Glasgow.
{C.fName, C.lName | Client(C) Ù (($V)($P)
(Viewing(V) Ù PropertyForRent(P) Ù
(C.clientNo = V.clientNo) Ù
(V.propertyNo=P.propertyNo) Ù
P.city =‘Glasgow’))}
Tuple Relational Calculus
{fN, lN | ($sN)
(Staff(sN,fN,lN,posn,sex,DOB,sal,bN)
(~($sN1) (PropertyForRent(pN, st, cty, pc, typ,
rms, rnt, oN, sN1, bN1) Ù (sN=sN1))))}
Example - Domain Relational
Calculus
• List the names of clients who have viewed a
property for rent in Glasgow.