RELATIONAL ALGEBRA AND RELATIONAL
CALCULUS
SELECT and PROJECT
unary operations that operate on single relations
The SELECT operation is used to choose a subset of the tuples from a relation that
satisfies a selection condition.
select the EMPLOYEE tuples whose department is 4, or
those whose salary is greater than $30,000, we can individually specify each of
these
<attribute name> <comparison op> <constant value>
or
<attribute name> <comparison op> <attribute name>
PROJECT
The PROJECT operation selects certain columns from the table and discards the
other columns.
The result of the PROJECT operation can be visualized as a vertical partition of the
relation into two relations.
The PROJECT operation removes any duplicate tuples, so the result of the PROJECT
operation is a set of distinct tuples, and hence a valid relation. This is known as
duplicate elimination.
Faculty
Class Dept Position
Assistant
5 CSE
Professor
Assistant
5 CSE
Professor
Assistant
6 EE
Professor
Assistant
6 EE
Professor
πClass, Dept(Faculty)
Class Dept
5 CSE
6 EE
Sequences of Operations and the RENAME operation
Inline expression(single line expression)
OR
Assignment Operation
• We can also define a formal RENAME operation—which can
rename either the relation name or the attribute names,
or both—as a unary operator.
The general RENAME operation when applied to a relation
R of degree n is denoted by any of the following three
forms:
where the symbol ρ (rho) is used to denote the RENAME operator, S is the new
relation name, and B1, B2, … , Bn are the new attribute names.
Relational Algebra Operations
from Set Theory: The UNION, INTERSECTION, and MINUS
Operations
UNION: The result of this operation, denoted by R ∪ S, is a relation that
includes all tuples that are either in R or in S or in both R and S. Duplicate
tuples are eliminated.
INTERSECTION: The result of this operation, denoted by R ∩ S, is a relation
that includes all tuples that are in both R and S.
SET DIFFERENCE (or MINUS): The result of this operation, denoted by R – S,
is a relation that includes all tuples that are in R but not in S.
The CARTESIAN PRODUCT (CROSS PRODUCT)
Operation
CARTESIAN PRODUCT operation—also known as CROSS PRODUCT or
CROSS JOIN—which is denoted by ×.
This is also a binary set operation
This set operation produces a new element by combining every
member (tuple) from one relation (set) with every member (tuple)
from the other relation (set).
In general, the result of R(A1, A2, … , An) × S(B1, B2, … , Bm) is a
relation Q with degree n + m attributes Q(A1, A2, … , An, B1, B2, … ,
Bm),
in that order.
if R has x tuples and S has y tuples, then R × S will have x * y tuples.
Binary Relational Operations:
JOIN and DIVISION
JOINS :Theta Join
Subjects
Student
Class Subject
SID Name Std
10 Math
101 Alex 10
10 English
102 Maria 11
11 Music
11 Sports
STUDENT ⋈Student.Std = Subject.Class SUBJECT
Student_detail
SID Name Std Class Subject
101 Alex 10 10 Math
101 Alex 10 10 English
102 Maria 11 11 Music
102 Maria 11 11 Sports
Natural Join
Courses HoD
CID Course Dept Dept Head
CS01 Database CS CS Alex
ME01 Mechanics ME ME Maya
EE01 Electronics EE EE Mira
Courses * HoD
Dept CID Course Head
CS CS01 Database Alex
ME ME01 Mechanics Maya
EE EE01 Electronics Mira
Left Outer Join(R S)
Left Right
A B A B
100 Database 100 Alex
101 Mechanics 102 John
102 Electronics 104 Mira
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 John
Right Outer Join: ( R S )
Left Right
A B A B
100 Database 100 Alex
101 Mechanics 102 John
102 Electronics 104 Mira
A B C D
100 Database 100 Alex
102 Electronics 102 John
--- --- 104 Mira
Full Outer Join: ( R S)
Left Right
A B A B
100 Database 100 Alex
101 Mechanics 102 John
102 Electronics 104 Mira
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 John
--- --- 104 Mira
The JOIN(Equijoin) operation, denoted by , is used to combine related tuples
from two relations into single “longer” tuples.
DBMS - Joins (tutorialspoint.com)
Natural Join
DIVISION OPERATION
Denoted by ÷
Retrieve the names of employees who work on all the projects in which John Smith
works.
Example:
QUERY TREES