Relational Algera
Relational Algera
Relational Algebra
Relational database systems are expected to be equipped with a query language that
can assist its users to query the database instances. There are two kinds of query
languages − relational algebra and relational calculus.
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.
SELECT (symbol: σ)
PROJECT (symbol: π)
RENAME (symbol: )
UNION (υ)
INTERSECTION ( ),
DIFFERENCE (-)
CARTESIAN PRODUCT ( x )
JOIN
DIVISION
σp(r)
σ is the predicate
p is prepositional logic
Example 1
Example 2
Output - Selects tuples from Tutorials where the topic is 'Database' and 'author' is guru99.
Example 3
Output - Selects tuples from Customers where sales is greater than 50000
Projection(π)
The projection eliminates all attributes of the input relation but those mentioned in the projection list. The projection
method defines a relation that contains a vertical subset of Relation.
This helps to extract the values of specified attributes to eliminates duplicate values. (pi) The symbol used to choose
attributes from a relation. This operation helps you to keep specific columns from a relation and discards the other
columns.
Example of Projection:
1 Google Active
2 Amazon Active
3 Apple Inactiv
e
4 Alibaba Active
Google Active
Amazon Active
Apple Inactive
Alibaba Active
Example
Table A Table B
1 1 1 1
1 2 1 3
A ∪ B gives
Table A ∪ B
column 1 column 2
1 1
1 2
1 3
Example
A-B
Table A - B
column 1 column 2
1 2
Intersection
An intersection is defined by the symbol ∩
A∩B
Defines a relation consisting of a set of all tuple that are in both A and B. However, A and B must be union-
compatible.
Example:
A ∩ B
Table A ∩ B
column 1 column 2
1 1
Cartesian product(X)
This type of operation is helpful to merge columns from two relations. Generally, a Cartesian product is never a
meaningful operation when it performs alone. However, it becomes meaningful when it is followed by other
operations.
σ column 2 = '1' (A X B)
Output – The above example shows all rows from relation A and B whose column 2 has value 1
σ column 2 = '1' (A X B)
column 1 column 2
1 1
1 1
Join Operations
Join operation is essentially a cartesian product followed by a selection criterion.
JOIN operation also allows joining variously related tuples from different relations.
Types of JOIN:
Inner Joins:
Theta join
EQUI join
Natural join
Outer join:
Inner Join:
In an inner join, only those tuples that satisfy the matching criteria are included, while the rest are excluded. Let's
study various types of Inner Joins:
Theta Join:
The general case of JOIN operation is called a Theta join. It is denoted by symbol θ
Example
A ⋈θ B
For example:
column 1 column 2
1 2
EQUI join:
When a theta join uses only equivalence condition, it becomes a equi join.
For example:
column 1 column 2
1 1
EQUI join is the most difficult operations to implement efficiently in an RDBMS and one reason why RDBMS have
essential performance problems.
NATURAL JOIN (⋈)
Natural join can only be performed if there is a common attribute (column) between the relations. The name and type
of the attribute must be same.
Example
Num Square
2 4
3 9
Num Cube
2 8
3 18
C ⋈ D
C⋈D
2 4 4
3 9 9
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria, we also include some or all tuples that do not
match the criteria.
Num Square
2 4
3 9
4 16
Num Cube
2 8
3 18
5 75
A B
A⋈B
2 4 4
3 9 9
4 16 -
Right Outer Join: ( A B)
In the right outer join, operation allows keeping all tuple in the right relation. However, if there is no matching tuple is
found in the left relation, then the attributes of the left relation in the join result are filled with null values.
A B
A⋈B
2 8 4
3 18 9
5 75 -
A B
A⋈B
2 4 8
3 9 18
4 16 -
5 - 75
Summary
Operation Purpose
Select(σ) The SELECT operation is used for selecting a
subset of the tuples according to a given selection
condition
Inner Join Inner join, includes only those tuples that satisfy
the matching criteria.
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.
Full Outer Join( ) In a full outer join, all tuples from both relations are
included in the result irrespective of the matching
condition.
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural query
language, that is, it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
Tuple Relational Calculus (TRC)
In DRC, the filtering variable uses the domain of attributes instead of entire tuple
values (as done in TRC, mentioned above).
Notation −
{ a , a , a , ..., a | P (a , a , a , ... ,a )}
1 2 3 n 1 2 3 n
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈ TutorialsPoint ∧ subject =
'database'}
Output − Yields Article, Page, and Subject from the relation TutorialsPoint, where
subject is database.
Just like TRC, DRC can also be written using existential and universal quantifiers. DRC
also involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation Calculus is
equivalent to Relational Algebra.