Lecture 9 10
Lecture 9 10
Marina Gul
Sukkur IBA University Khairpur Campus
2 Agenda
The Relational Algebra
Select Operation
Project Operation
Union Operation
Set Difference Operation
Set intersection Operation
Cartesian Product Operation
Rename Operation
Join Operations
3
Function of the relational algebra
operations
4 Composition of Operations
Can build expressions using multiple
operations
Example: A=C (r x s)
rxs
A=C (r x s)
5 Join Operations
Join is a derivative of Cartesian product.
Equivalent to using join predicate as selection formula, over Cartesian product of
the two operand relations.
There are various forms of the Join operation, each with subtle differences, some
more useful than others:
• Theta join
• Equijoin (a particular type of Theta join)
• Natural join
• Outer join
• Semijoin
6 Theta join ( -join) R F S
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
(<, , >, , =, ).
Can rewrite Theta join using basic Selection and Cartesian product operations. R F S=
F(R S)
Degree of a Theta join is sum of degrees of the operand relations R and S. If predicate F
contains only equality (=), the term Equijoin is used.
Example: List the names and comments of all clients who have viewed a property for rent.
Example: List the names and comments of all clients who have viewed a property
for rent.
R F S = A(R F S)
T1 C(R)
T2 C((S X T1) – R)
T T1 – T2
Example: Identify all clients who have viewed all properties with three rooms.
(clientNo, propertyNo(Viewing)) (propertyNo(rooms = 3 (PropertyForRent)))
1
8
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.
1
9
Practice
How many properties cost more than £350 per month to rent?
Resulting relation contains the grouping attributes, GA, along with results of each of the
aggregate functions.
2
1
Practice
Find the number of staff working in each branch and the sum of their salaries.