Professional Documents
Culture Documents
L1.0 Relational Algebra
L1.0 Relational Algebra
Relational Algebra allows us to translate declarative (SQL) queries into precise and
optimizable expressions!
Relational Algebra Operators
Relational algebra operators
Five basic operators
Operator Symbol
σ
Selection
π
Projection
×
Cartesian Product
Ս
Union
-
Difference
Relational Algebra Operators
Relational algebra operators
Derived or auxiliary operators
Intersection, complement
Renaming:
Division
Relational Algebra Basic Operators
Relational Algebra Operators
Relational algebra operators
1. Selection ( σ )
Syntax:
σ condition ( Relation___ )
Selection ( σ )
Example: Given the following students relation, write a relation algebra statement to
STUDENTS
Student_name Marks
John 20
Fridah 80
Joseph 90
Relational Algebra Operators
SOLUTION Example: List all students who scored
more than 40 marks
Selection ( σ )
condition SELECT *
FROM Students
Notation: σ condition ( Relation___ ) WHERE marks > 40;
Relational Algebra
2. Projection ( π)
Eliminates columns, then removes duplicates
Projection ( π)
Example: Given the following students relation, write a relation algebra statement to
STUDENTS
Student_name Marks
John 20
Fridah 80
Joseph 90
Relational Algebra Operators
SOLUTION Example: Given the students relation,
display all student names
Projection ( π)
Reduces the number of columns SQL
Relational Algebra
π student_name (Students)
Relational Algebra Operators
Relational algebra operators combined (Example)
Solution
𝜎 𝑚𝑎𝑟𝑘𝑠>40( (Students))
Relational Algebra Operators
If one relation has I tuples and N attributes and the other has J tuples and M
+ M) attributes.
Notation: R × S
Where R and S represent relations
Relational Algebra Operators
SELECT *
FROM Students, People;
Students X People
Relational Algebra
Students X People;
If the two relations have attributes with the same name, the names are prefixed with the relation name to
maintain the uniqueness of attribute names within a relation.
Relational Algebra Operators
Cross-product / Cartesian Product (×)
Example: List the names and comments of all clients who have viewed a property for rent.
• The names of clients are held in the Client relation and the details of viewings are held in the
Viewing relation.
• To obtain the list of clients and the comments on properties they have viewed , we need to combine
these two relations.
• To obtain the required list, we need to carry out a Selection operation on this relation to extract those
tuples where Client.clientNo = Viewing.clientNo.
• The complete operation is thus:
Result
Result :
Relational Algebra Operators
4. Renaming (𝜌 )
We can use the Rename operation ρ (rho), which gives a name to the result
Rename allows an optional name for each of the attributes of the new relation
to be specified.
The Rename operation ( ρ), provides a new name S for the expression E, (ρ ( E ) ) S
and optionally names the attributes as a1, a2, . . . , an. ( ρ S (a1, a2, . . . , a n) (E))
Relational Algebra Operators
4. Renaming (𝜌 ) (Example)
Relation R Relation S
• Suppose, however, that we do not wish to
Cartesian
RXS the name B for the attribute that comes
first is called X.
Relational Algebra Operators
4. Renaming (𝜌 ) (Example Continued … (1))
Relation S
ρ S (X,C,D) (S)
Initial relation name
S in new name of relation,
X,C, D are new names of
the attributes
Relational Algebra Operators
4. Renaming (𝜌 ) (Example Continued … (2))
Relation S
New Relation S Relation R
Relation R
RXS
RXS
Same as:
R x ( ρ S (X,C,D) (S) )
Relational Algebra (JOINS)
What you’ll learn
Natural Join
Theta Join
Equi-Join
Relational Algebra (JOINS)
1. Natural Join (⋈ )
The natural join of two relations R and S, denoted R ⋈ S, in which we pair only those tuples
from R and S that agree in whatever attributes are common to the schemas of R and S.
If the tuples r and s are successfully paired in the join R ⋈ S, then the result of the pairing is a
tuple, called the joined tuple, with one component for each of the attributes in the union of the
schemas of R and S.
Relational Algebra (JOINS)
1. Natural Join (⋈ ) (Example 1)
Example
Relation S The only attribute common to R and S is B.
Relation R
2. Theta Joins (⋈ )
• Select from the product only those tuples that satisfy the condition C.
Relational Algebra (JOINS)
• See whether the A component from
2. Theta Joins (Example)
the U tuple is less than the D
component of the V tuple.
Consider the operation U ⋈ a < d V,
Relation U Relation V • The first tuple of U, with an A
component of 1, successfully pairs
with each of the tuples from V.
• However, the second and third
tuples from U, with A components
of 6 and 9, respectively, pair
U ⋈ a<dV
successfully with only the last
tuple of V.
Example:
Employee ⋈ SSN=SSN Dependents