DBMS - Relational Algebra (1)
DBMS - Relational Algebra (1)
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
Project
Union
Set different
Cartesian product
Rename
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 −
https://www.tutorialspoint.com/dbms/relational_algebra.htm 1/5
5/7/24, 12:40 PM DBMS - Relational Algebra
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or
those books published after 2010.
Notation − ∏A , A , A (r)
1 2 n
For example −
Selects and projects columns named as subject and author from the relation Books.
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).
https://www.tutorialspoint.com/dbms/relational_algebra.htm 2/5
5/7/24, 12:40 PM DBMS - Relational Algebra
Output − Projects the names of the authors who have either written a book or an
article or both.
Notation − r − s
Output − Provides the name of authors who have written books but not articles.
Notation − r Χ s
r Χ s = { q t | q ∈ r and t ∈ s}
Output − Yields a relation, which shows all the books and articles written by
tutorialspoint.
https://www.tutorialspoint.com/dbms/relational_algebra.htm 3/5
5/7/24, 12:40 PM DBMS - Relational Algebra
Notation − ρ x (E)
Set intersection
Assignment
Natural join
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.
Notation − {T | Condition}
For example −
Output − Returns tuples with 'name' from Author who has written article on
'database'.
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
For example −
https://www.tutorialspoint.com/dbms/relational_algebra.htm 4/5
5/7/24, 12:40 PM DBMS - Relational Algebra
Output − The above query will yield the same result as the previous one.
In DRC, the filtering variable uses the domain of attributes instead of entire tuple
values (as done in TRC, mentioned above).
Notation −
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
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.
https://www.tutorialspoint.com/dbms/relational_algebra.htm 5/5