Relational Algebra Examples
Relational Algebra Examples
Relational Algebra Examples
Archana Gupta
CS 157
What is Relational Algebra?
Relational Algebra is formal description of
how relational database operates.
It is a procedural query language, i.e. user
must define both “how” and “what” to
retrieve.
It consists of a set of operators that
consume either one or two relations as
input. An operator produces one relation
as its output.
Introduction to Relational Algebra
Introduced by E. F.
Codd in 1970.
Unary Operations
- operate on one
relation. These
include select,
project and
rename
operators.
Binary Operations
- operate on pairs
of relations.
Select Operator
The Select operator selects
tuples that satisfies a
predicate; e.g. retrieve the
employees whose salary is
30,000
б Salary = 30,000 (Employee)
Conditions in Selection:
Simple Condition: (attribute)
(comparison)(attribute)
(attribute)
(comparison)
(constant)
Comparison: =,≠,≤,≥,<,>
Select Operator Example
Person бAge≥34(Person)
Name Age Weight
Name Age Weight
Harry 34 80
Harry 34 80 Helena 54 54
Sally 28 64 Peter 34 80
George 29 70
Helena 54 54 бAge=Weight(Person)
Peter 34 80 Name Age Weight
Helena 54 54
Project Operator
Project (∏) retrieves a column. Duplication
is not permitted.
Sally 28 90,000
George 29 70,000 ∏ name(бSalary>80,000(Employee))
Helena 54 54,280
Name
Peter 34 40,000
Sally
Cartesian Product
In mathematics, it is a set of all pairs of
elements (x, y) that can be constructed
from given sets, X and Y, such that x
belongs to X and y to Y.
Student Professor
FN LN
Susan Yao
Ramesh Shah
Set Difference Operator
For relations R and S,
Professor - Student
Student - Professor
FN LN
FN LN
John Smith
Barbara Jones
Ricardo Brown
Amy Ford
Francis Johnson
Jimmy Wang
Division Operator
The division operator takes as input two
relations, called the dividend relation (r on
scheme R) and the divisor relation (s on
scheme S) such that all the attributes in S also
appear in R and S is not empty. The output of
the division operation is a relation on scheme R
with all the attributes common with S.
Division Operator Example
Completed DBProject
Student Task Task
Fred Database1 Database1
Fred Database2
Database2
Fred Compiler1
Eugene Database1
Sara Database1
Sara Database2
Eugene Compiler1
Completed / DBProject
Student
Fred
Sara
Natural Join Operator
Natural join is a dyadic operator that is written
as R lXl S where R and S are relations. The
result of the natural join is the set of all
combinations of tuples in R and S that are
equal on their common attribute names.
Natural Join Example
For an example, consider the tables Employee and Dept and their
natural join:
Employee
Name EmpID DeptName
Harry 3415 Finance
Sally 2241 Sales
George 3401 Finance
Harriet 2202 Sales
Employee
Name EmpID DeptName
Harry 3415 Finance
Sally 2241 Sales
George 3401 Finance
Harriet 2202 Sales
Employee ⋉ Dept
Dept Name EmpID DeptName
DeptName Mgr
Sally 2241 Sales
Sales Harriet
Harriet 2202 Sales
Production Charles
Outerjoin Operator
Left outer join
The left outer join is written as R =X S where R
and S are relations. The result of the left outer
join is the set of all combinations of tuples in R
and S that are equal on their common attribute
names, in addition to tuples in R that have no
matching tuples in S.
Right outer join
The right outer join is written as R X= S where
R and S are relations. The result of the right
outer join is the set of all combinations of tuples
in R and S that are equal on their common
attribute names, in addition to tuples in S that
have no matching tuples in R.
Left Outerjoin Example
For an example consider the tables Employee and Dept and their left outer
join:
Employee
Name EmpID DeptName
Harry 3415 Finance
Sally 2241 Sales
George 3401 Finance Employee =X Dept
Harriet 2202 Sales Name EmpID DeptName Mgr
Employee
Name EmpID DeptName
Harry 3415 Finance
Employee =X= Dept
Sally 2241 Sales
Name EmpID DeptName Mgr
George 3401 Finance
Harriet 2202 Sales Harry 3415 Finance ω
Sally 2241 Sales Harriet
Dept George 3401 Finance ω
DeptName Mgr
Harriet 2202 Sales Harriet
Sales Harriet
ω ω Production Charles
Production Charles
Division