Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Relational Algebra

What you’ll Learn


 Relational Algebra

◎ Quality assurance and standards


◎ Quality planning and control
Relational Algebra basics
RDBMS Architecture
How does SQL engine work?

Declarative Translate to Find logically Execute each


query (from relational algebra equivalent- but more operator of the
user) expression efficient- RA optimized plan!
expression
RDBMS Architecture
How does SQL engine work?

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

 Joins ( natural , equi-join , theta join , semi-join )

 Renaming:

 Division
Relational Algebra Basic Operators
Relational Algebra Operators
Relational algebra operators

1. Selection ( σ )

 Returns all tuples which satisfy a condition

Syntax:
σ condition ( Relation___ )

Relation : The name of the table

Condition : can be = , < ,  >= , > , <= , <>


Relational Algebra Operators

Selection ( σ )

 Example: Given the following students relation, write a relation algebra statement to

display all students who scored more than 40 marks

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 ( σ )

 Returns all tuples which satisfy a SQL

condition SELECT *
FROM Students
Notation: σ condition ( Relation___ ) WHERE marks > 40;

Relational Algebra

σ marks > 40 (Students)


Relational Algebra Operators

2. Projection ( π)
 Eliminates columns, then removes duplicates

 Reduces the number of columns displayed in the result.

Notation: π columns ( Relation___ )

Columns: Names of columns to be included in the result

Relation: Name of the table from which columns are displayed


Relational Algebra Operators

Projection ( π)
 Example: Given the following students relation, write a relation algebra statement to

display all students names.

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

displayed in the result. SELECT student_name


FROM Students
Notation: π columns ( Relation )

Relational Algebra

π student_name (Students)
Relational Algebra Operators
Relational algebra operators combined (Example)

 Consider the following SQL statement:

SELECT student_name, marks


FROM Students
WHERE marks > 40;

How do we represent this SQL statement in Relational Algebra?


Relational Algebra Operators
Relational algebra operators combined (Example Continued …)

Solution

π student_name, marks (σ marks>40 (Students))

SELECT student_name, marks


FROM Students
WHERE marks > 40;
Equivalent
statements

How do we represent this SQL statement in Relational Algebra?

𝜎 𝑚𝑎𝑟𝑘𝑠>40(  (Students))
Relational Algebra Operators

3. Cross-product / Cartesian Product (×)

 Defines a relation that is the concatenation of every tuple of relation R with

every tuple of relation S.

 If one relation has I tuples and N attributes and the other has J tuples and M

attributes, the Cartesian product relation will contain (I * J) tuples with (N

+ M) attributes.

Notation: R × S
Where R and S represent relations
Relational Algebra Operators

Cross-product / Cartesian Product (×)

 Example: Consider the relations people and students


SQL

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.

( π clientNo, fName, lName (Client)) × ( π clientNo, propertyNo, comment(Viewing))

• 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:

σClient.clientNo = Viewing.clientNo ( (πclientNo, fName, lName(Client)) × ( π clientNo, propertyNo, comment (Viewing)))


Relational Algebra Operators
Example (Explained): List the names and comments of all clients who have viewed a property for rent

( π clientNo, fName, lName (Client)) × ( π clientNo, propertyNo, comment(Viewing))

Result

• In its present form, this result has


more information than required.

• 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.
Relational Algebra Operators
Example (Explained): List the names and comments of all clients who have viewed a
property for rent
• After carrying out the the selection operation, to extract those tuples where
Client.clientNo = Viewing.clientNo the complete operation is thus:

σClient.clientNo = Viewing.clientNo ( (πclientNo, fName, lName(Client)) × ( π clientNo, propertyNo, comment (Viewing)))

Result :
Relational Algebra Operators

4. Renaming (𝜌 )

 We can use the Rename operation ρ (rho), which gives a name to the result

of a relational algebra operation.

 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

call the two versions of B by names R.B

and S.B; rather we want to continue to use

Cartesian
RXS the name B for the attribute that comes

product of from R, and we want to use X as the name


R and S
results to:
of the attribute B coming from S.

• We can rename the attributes of S so the

first is called X.
Relational Algebra Operators
4. Renaming (𝜌 ) (Example Continued … (1))

Relation S

• To rename the attributes of S so the first is called X.


New 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

 Thus, to pair successfully, tuples need only to


agree in their B components.

R⋈S  If so, the resulting tuple has components for


attributes A (from R), B (from either R or S), C
(from S), and D (from S).
Relational Algebra (JOINS)
1. Natural Join (⋈ ) ( Example 2 )
Relation U Relation V
• For tuples to pair successfully, they
must agree in both the B and C
components.

• Thus, the first tuple of U joins with


the first two tuples of V, while the
U⋈V second and third tuples of U join
with the third tuple of V.

Exercise: Rewrite the natural join of U and V


in terms of cross product , projection and
selection
Relational Algebra (JOINS)

2. Theta Joins (⋈  )

 The notation for a theta-join of relations R and S based on condition C is R ⋈ c S.

 The result of this operation is constructed as follows:

• Take the product of R and S.

• 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.

• Thus, the result has only five tuples,


constructed from the five successful
pairings
Relational Algebra (JOINS)
3. Equijoin ( ⋈ A=B )

 An equijoin is a theta join with an equality condition

R1 ⋈ A=B R2 = σ A=B (R1 x R2)

Example:
Employee ⋈ SSN=SSN Dependents

You might also like