Relational Algebra
Relational Algebra
Presented by
Mr. P. Arul Selvam
AP/CSE/HICET
1
Relational Algebra
3
Educate Ethics Excel
Select Operation
• The select operation selects tuples that satisfy a given predicate.
• Notation: p (r)
• p is called the selection predicate
• Example: select those tuples of the instructor relation where the instructor is in the
“Physics” department.
– Query
dept_name=“Physics” (instructor)
– Result
4
Educate Ethics Excel
Select Operation (Cont.)
• We allow comparisons using
=, , >, . <.
in the selection predicate.
• We can combine several predicates into a larger predicate by using the connectives:
(and), (or), (not)
• Example: Find the instructors in Physics with a salary greater $90,000, we write:
5
Educate Ethics Excel
Project Operation
• A unary operation that returns its argument relation, with certain attributes left out.
• Notation:
6
Educate Ethics Excel
Project Operation Example
• Example: eliminate the dept_name attribute of instructor
• Query:
7
Educate Ethics Excel
Composition of Relational Operations
• The result of a relational-algebra operation is relation and therefore of relational-
algebra operations can be composed together into a relational-algebra expression.
• Consider the query -- Find the names of all instructors in the Physics department.
• Instead of giving the name of a relation as the argument of the projection operation,
we give an expression that evaluates to a relation.
8
Educate Ethics Excel
Cartesian-Product Operation
• The Cartesian-product operation (denoted by X) allows us to combine information
from any two relations.
• Example: the Cartesian product of the relations instructor and teaches is written as:
instructor X teaches
• We construct a tuple of the result out of each possible pair of tuples: one from the
instructor relation and one from the teaches relation (see next slide)
• Since the instructor ID appears in both relations we distinguish between these attribute
by attaching to the attribute the name of the relation from which the attribute
originally came.
– instructor.ID
– teaches.ID
9
Educate Ethics Excel
The instructor X teaches table
10
Educate Ethics Excel
Join Operation
• The Cartesian-Product
instructor X teaches
associates every tuple of instructor with every tuple of teaches.
– Most of the resulting rows have information about instructors who did NOT teach
a particular course.
• To get only those tuples of “instructor X teaches “ that pertain to instructors and the
courses that they taught, we write:
– We get only those tuples of “instructor X teaches” that pertain to instructors and
the courses that they taught.
• The result of this expression, shown in the next slide
11
Educate Ethics Excel
Join Operation (Cont.)
• The table corresponding to:
12
Educate Ethics Excel
Join Operation (Cont.)
• The join operation allows us to combine a select operation and a Cartesian-Product
operation into a single operation.
• Consider relations r (R) and s (S)
• Let “theta” be a predicate on attributes in the schema R “union” S. The join operation
r s is defined as follows:
• Thus
13
Educate Ethics Excel
Union Operation
• The union operation allows us to combine two relations
• Notation: r s
• For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd
column of r deals with the same type of values as does the
2nd column of s)
• Example: to find all courses taught in the Fall 2017 semester, or in the Spring 2018 semester, or in
both
14
Educate Ethics Excel
Union Operation (Cont.)
• Result of:
15
Educate Ethics Excel
Set-Intersection Operation
• The set-intersection operation allows us to find tuples that are in both the input
relations.
• Notation: r s
• Assume:
– r, s have the same arity
– attributes of r and s are compatible
• Example: Find the set of all courses taught in both the Fall 2017 and the Spring 2018
semesters.
16
Educate Ethics Excel
Set Difference Operation
• The set-difference operation allows us to find tuples that are in one relation but are not
in another.
• Notation r – s
• Set differences must be taken between compatible relations.
– r and s must have the same arity
– attribute domains of r and s must be compatible
• Example: to find all courses taught in the Fall 2017 semester, but not in the Spring 2018
semester
course_id ( semester=“Fall” Λ year=2017 (section)) −
course_id ( semester=“Spring” Λ year=2018 (section))
17
Educate Ethics Excel
The Assignment Operation
• It is convenient at times to write a relational-algebra expression by assigning parts of it
to temporary relation variables.
• The assignment operation is denoted by and works like assignment in a
programming language.
• Example: Find all instructor in the “Physics” and Music department.
18
Educate Ethics Excel
The Rename Operation
• The results of relational-algebra expressions do not have a name that we can use to
refer to them. The rename operator, , is provided for that purpose
• The expression:
x (E)
returns the result of expression E under the name x
• Another form of the rename operation:
19
Educate Ethics Excel
Thank
you
19CS4201 / Java Programming 20
Educate Ethics Excel