Relational Algebra
Relational algebra is a procedural query language, which takes instances of relations as
input and yields instances of relations as output. It uses operators to perform queries. An
operator can be either unary or binary. They accept relations as their input and yield
relations as their output. Relational algebra is performed recursively on a relation and
intermediate results are also considered relations.
The fundamental operations of relational algebra are as follows −
Select
Project
Union
Set different
Cartesian product
Rename
We will discuss all these operations in the following sections.
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic
formula which may use connectors like and, or, and not. These terms may use relational
operators like − =, ≠, ≥, < , >, ≤.
For example −
σsubject = "database" (Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450" (Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject (Books)
= "database" and price = "450" or year > "2010"
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those
books published after 2010.
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A , A , A (r)
1 2 n
Where A , A , A are attribute names of relation r.
1 2 n
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
Union Operation (∪)
It performs binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −
r, and s must have the same number of attributes.
Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article or
both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one relation but are not
in the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor (Books Χ Articles)
= 'tutorialspoint'
Output − Yields a relation, which shows all the books and articles written by tutorialspoint.
Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The rename
operation allows us to rename the output relation. 'rename' operation is denoted with small
Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Summary
Operation Purpose
Select(σ) The SELECT operation is used for
selecting a subset of the tuples
according to a given selection
condition
Projection(π) The projection eliminates all attributes
of the input relation but those
mentioned in the projection list.
Union Operation(∪) UNION is symbolized by symbol. It
includes all tuples that are in tables A
or in B.
Set Difference(-) - Symbol denotes it. The result of A -
B, is a relation which includes all
tuples that are in A but not in B.
Intersection(∩) Intersection defines a relation
consisting of a set of all tuple that are
in both A and B.
Cartesian Product(X) Cartesian operation is helpful to merge
columns from two relations.
Inner Join Inner join, includes only those tuples
that satisfy the matching criteria.
Theta Join(θ) The general case of JOIN operation is
called a Theta join. It is denoted by
symbol θ.
EQUI Join When a theta join uses only
equivalence condition, it becomes a
equi join.
Natural Join(⋈) Natural join can only be performed if
there is a common attribute (column)
between the relations.
Outer Join In an outer join, along with tuples that
satisfy the matching criteria.
Left Outer Join( ) In the left outer join, operation allows
keeping all tuple in the left relation.
Right Outer join( ) In the right outer join, operation allows
keeping all tuple in the right relation.
Full Outer Join( ) In a full outer join, all tuples from both
relations are included in the result
irrespective of the matching condition.