Relational Algebra
Relational Algebra
Relational Algebra
Lecture 13
Relational Algebra
• It is a procedural query language which uses collection of ‘OPERATORS’
to compose the queries.
Operation on
1&2
Relation 4
Relation 3
Operation on
3&4
Relation 5
Why is it still useful?
• SELECT (symbol: σ)
Unary Relational Operations (applied on single table)
• PROJECT (symbol: π)
• RENAME (symbol: ρ)
• UNION (υ)
• INTERSECTION ( ), Relational Algebra Operations From Set Theory
• DIFFERENCE (-)
• CARTESIAN PRODUCT ( x )
• JOIN
Binary Relational Operations
• DIVISION
1. Select Operation:
• The select operation selects tuples that satisfy a given predicate.
• It is denoted by sigma (σ).
• It provide rows as a results
• Where:
• σ is used for selection prediction
• r is used for relation
• p is used as a propositional logic formula which may use connectors like: AND OR
and NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
<attribute name> <comparison operator> <constant value> or <attribute name>
Example
Student
Selects tuples from books where subject is 'database' and 'price' is 450.
Selects tuples from books where subject is 'database' and 'price' is 450 or those books published
after 2010.
Project
• It projects column(s) that satisfy a given predicate.
• Duplicate rows are automatically eliminated, as relation is a set.
• (π) symbol is used to choose attributes from a relation.
• This operator helps you to keep specific columns from a relation and
discards the other columns.
π A (R)
symbol ‘π(pi)’ is used to denote the Project operator
where ‘A’ is the attribute list, it is the desired set of attributes from the attributes of
relation(R),
R is generally a relational algebra expression, which results in a relation.
Example Display Class and Dept from
Faculty
Faculty
Faculty
Faculty
ρnewname (tablename)
Student
The student table is renamed with newstudent with the help of the following command −
ρnewstudent (student)
The name, branch column of student table are renamed and newbranch respectively
ρnewname,newbranch(∏name,branch( student))
Example-1: Query to rename the relation Student as Male Student and the attributes of
Student – Roll, Name as (Sno, SName).
#Cols = M+N
#Rows = M*N
Provides the name of authors who have written books but not articles.
Union Operation
• The union operation contains all the tuples that are either in R
or S or both in R & S.
• It eliminates the duplicate tuples. It is denoted by ∪.
• A union operation must hold the following condition:
• R and S must have the attribute of the same number.
• Duplicate tuples are eliminated automatically.
Example
• Projects the names of the authors who have either written a
book or an article or both.
BORROW RELATION DEPOSITOR
RELATION
A(X,Y)/ B(Y)
Finds records in R1 that is that is related to all related to all records in records in R2
– Related is measured based on attributes with same name.
Division Operator (÷)
• Division operator can be applied to if and only if
Attributes of B is proper subset of attributes of A
The relation returned by division operator will have attributes = (All
attributes of A - All attributes of B)
The relation returned by division operator will return those tuples from
relation have which are associated to every B’s tuple.
Example of Division A/B
A/B1
S1->P2
S1->P4
S4->P2
S4->P4
StudentEnrolled
Sid Cid Course
S1 C1 Cid
S2 C1 C1
S1 C2 C2
S3 C2
Student
Hobby
Expressing A/B using basic operators
• Division can be expressed using cross product, set-difference and
projection
- =
Join Operations
• Join operation is essentially a cartesian product followed by a
selection criterion.
• For example:
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.
R1 ⋈ R2
∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
Left outer join
• Left outer join contains the set of tuples of all combinations in A and B that
are equal on their common attribute names.
• In the left outer join, tuples in A have no matching tuples in B.
• It is denoted by ⟕.
Right outer join
• Right outer join contains the set of tuples of all combinations in
R and S that are equal on their common attribute names.
• In right outer join, tuples in S have no matching tuples in R.
• It is denoted by ⟖.
Full outer join
• Full outer join is like a left or right join except that it contains all
rows from both tables.
• In full outer join, tuples in R that have no matching tuples in S
and tuples in S that have no matching tuples in R in their
common attribute name.
• It is denoted by ⟗.