Relational Algebra
Relational Algebra
Relational Algebra
Query Language
• A Language which is used to store and retrieve
data from database is known as query
language. For example – SQL
• There are two types of query language:
1. Procedural Query language
2. Non-procedural query language
Procedural Query language
• The user instructs the system to perform a
series of operations to produce the desired
results.
• Here users tells what data to be retrieved
from database and how to retrieve it.
• For example – Making a cup of tea. If you are
telling the step by step process like switch on
the stove, boil the water, add milk etc. then it
is a procedural language.
Non Procedural Query Language
• The user instructs the system to produce the
desired result without telling the step by step
process.
• Here users tells what data to be retrieved
from database but doesn’t tell how to retrieve
it.
• Example: if you are just telling someone to
make a cup of tea and not telling the process
Conceptual Query Language
• Theoretical mathematical system or query language, to store
and retrieve data is called as Conceptual Query Language.
• They are not the practical implementation.
Types:
– Conceptual Procedural Query Language. E.g., Relational
Algebra
– Conceptual non-procedural query language. E.g., Relational
calculus
• Relational algebra and calculus are the theoretical concepts
used on relational model.
• SQL is a practical implementation of relational algebra and
calculus.
Relational Algebra
Relational Algebra
• The relational algebra is a procedural query
language or formal query language.
• It consists of a set of operations that take one or
two relations as input and produce a new relation
as their result.
• The select, project, and rename operations are
called unary operations, because they operate on
one relation.
• The other operations operate on pairs of
relations and are therefore called binary
operations.
Operators
• Basic Operators
– Projection ( )
– Selection ( )
– Cross Product (X)
– Union ( U)
– Rename ( )
– Set Difference (-)
• Derived Operators
– Join ( )
– Intersection ()
– Division ( / )
Project Operation – selection of
columns (Attributes)
• Relation r:
• A,C (r)
No duplicate data
Project Operation – selection of
columns (Attributes)
• Relation Student:
• roll=‘2’ (Student) 2 B 21
A B C C D E
1 2 3 3 4 5
2 1 4 2 1 2
• r x s:
Cartesian-product – naming issue
Relations r, s: B
r x s: r.B s.B
Union of two relations
• Relations r, s:
r s:
No duplicate values
Union of two relations
• Relations student, employee:
student Employee
The no. of columns in both the tables should be same
If Domain of attributes are same, then only union operation take place
student employee:
Roll Name
1 A
2 B Column name should
be similar to the 1st
3 C
table (left hand side of
7 E the union operator)
Union of two relations
• Relations student, employee:
student Employee
name (student) name (employee): The person who are student or employee
or both
Name
A
B
C
E
Renaming a Table
• Allows us to refer to a relation, (say E) by more than
one name.
x (E)
returns the expression E under the name X
• Relations r
• r x s (r)
Product of similar tables
(r,s) but different name
r x s (r)=(r X s)= r X r
Set difference of two relations
• Relations r, s:
r – s:
student Employee
The no. of columns in both the tables should be same
If Domain of attributes are same, then only difference operation take place
student - employee:
Roll Name
Find the name of the 2 B Column name should be
person who are student 3 C similar to the 1st table (left
but not employee hand side of the
difference operator)
Set difference of two relations
• Relations student, employee:
student Employee
name (student) - name (employee): The person who are student but not
employee
Name
B
C
Joining two relations – Natural Join
• Let r and s be relations on schemas R and S
respectively.
Then, the “natural join” of relations R and S is a
relation on schema R S obtained as follows:
– Consider each pair of tuples tr from r and ts
from s.
– If tr and ts have the same value on each of the
attributes in R S, add a tuple t to the result,
where
• t has the same value as tr on r
• t has the same value as ts on s
Natural Join Example
• Relations r, s:
A B C C D
1 2 3 2 6
2 1 2 4 6
3 4 5
Natural Join
r s
1 2 3 2 6
2 1 2 4 6
3 4 5
Natural Join
r s
A B R.C S.C D
1 2 3 2 6
A, r.B, C, r.D, E ( r.C = s.C (r x s)))
1 2 3 4 6
2 1 2 2 6
2 1 2 4 6
3 4 5 2 6
3 4 5 4 6
Natural Join Example
• Relations r, s:
A B C C D
1 2 3 2 6
2 1 2 4 6
3 4 5
Natural Join
r s
A B R.C S.D D
1 2 3 2 6
A, r.B, C, r.D, E ( r.C = s.C (r x s)))
1 2 3 4 6
2 1 2 2 6
2 1 2 4 6
3 4 5 2 6
3 4 5 4 6
Natural Join Example
• Relations r, s:
A B C C D
1 2 3 2 6
2 1 2 4 6
3 4 5
Natural Join
r s A B R.C D
2 1 2 6
A, r.B, C, r.D, E ( r.C = s.C (r x s)))
Join
• Join is a special form of cross product of two tables.
customers
customers orders
Production Charles
Select * from employee FULL
Dept
OUTER JOIN department ON
employee.DepatName=Dept.De
ptName
Anti Join
• The antijoin written as R ▷S where R and S are relations, is similar to the natural
join, but the result of an antijoin is only those tuples in R for which there is no
tuple in S that is equal on their common attribute names.
Employee
Name EmpID DeptName
Harry 3415 Finance
Sally 2241 Sales DeptName EmpId DeptName
George 3401 Finance Harry 3415 Finance
Harriet 2202 Sales George 3401 Finance
Dept
DeptName Mgr
Sales Harriet
Production Charles
Semi Join
• The left semijoin is joining similar to the natural join and written as R⋉ S where R and S are
relation.
• The result of this semijoin is only the set of all tuples in R for which there is a tuple in S that is
equal on their common attribute names.
Employee
Name EmpID DeptName
Harry 3415 Finance
Sally 2241 Sales
Name EmpID DeptName
George 3401 Finance
Sally 2241 Sales
Harriet 2202 Sales
Harriet 2202 Sales
Dept
DeptName Mgr
Sales Harriet
Production Charles
Set intersection of two relations
• Relation r, s:
• rs
student Employee
name (student) name (employee): The person who are student and also
employee
Name
A
Set Division of two relations
• Retrieve Sid of students who enrolled in every course. (at all, for all-division is
used)
Sid Cid Cid
S1 C1 C1
A(Sid,Cid)/B(Cid)
S2 C1 C2
Enrolled(Sid,Cid)/Course(Cid)
S1 C2 Course =Sid
S3 C2
Enrolled
• A(x,y)/B(y)= it results x values: for that there should be a tuple <x,y> in A for every
y value of relation B.
• Step 1 : Sid (Enrolled) X cid (Course)
It results all students are enrolled in every
S1 X C1 courses
S2 C2
S3
Set Division of two relations
• Retrieve Sid of students who enrolled in Step 2: Result
every course. (Sid (Enrolled) X cid (Course)) – Enrolled
Sid Cid Cid
S1 C1
S1 C1 C1 S1 C2
S2 C1 C2 S2 C1 S2 C2
S2 C2 = S3 C1
S1 C2 Course
S3 C1 It provides d the set of
S3 C2 S3 C2 students and their not
Enrolled enrolled courses
Step 3:
• Step 2:
Sid((Sid (Enrolled) X cid (Course)) – Enrolled)
(Sid (Enrolled) X cid (Course)) – Enrolled
S2
S1 C1 S1 C1
S3 It provides Sid of disqualified
Step 4: students
S1 C2 S2 C1
Sid (Enrolled)- (Sid((Sid (Enrolled) X cid (Course)) –
S2 C1 S1 C2 Enrolled))
S2 C2 S3 C2
S3 C1 S1 S2
S3 C2 S2 - S3 = S1 S1 enrolled in every course
S3
Composition of Operations
• Can build expressions using multiple operations
• Example: A=C (r x s)
• rxs
• A=C (r x s)
Summary of Relational Algebra Operators
Symbol (Name) Example of Use
σ
(Selection) σ salary > = 85000 (instructor)
Return rows of the input relation that satisfy the predicate.
Π
(Projection) Π ID, salary (instructor)
Output specified attributes from all rows of the input relation. Remove
duplicate tuples from the output.
x
(Cartesian Product) instructor x department
Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.
∪
(Union) Π name (instructor) ∪ Π name (student)
Output the set difference of tuples from the two input relations.
⋈
(Natural Join) instructor ⋈ department
Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.
Database System Concepts - 6th Edition 2.43 ©Silberschatz, Korth and Sudarshan
MCQs
A_____ is a query that retrieves rows from more
than one table or view:
a) Start
b) End
c) Join
d) All of the mentioned
MCQs
A_____ is a query that retrieves rows from more
than one table or view:
a) Start
b) End
c) Join
d) All of the mentioned
MCQs
Which product is returned in a join query have
no join condition:
a) Equijoins
b) Cartesian
c) Both Equijoins and Cartesian
d) None of the mentioned
MCQs
Which product is returned in a join query have
no join condition:
a) Equijoins
b) Cartesian
c) Both Equijoins and Cartesian
d) None of the mentioned
MCQs
Which join refers to join records from the right
table that have no matching key in the left table
are include in the result set:
a) Left outer join
b) Right outer join
c) Full outer join
d) Half outer join
MCQs
Which join refers to join records from the right
table that have no matching key in the left table
are include in the result set:
a) Left outer join
b) Right outer join
c) Full outer join
d) Half outer join
MCQs
Relation R1 has 10 tuples and 5 attributes.
Relation R2 has 0 tuples and 7 attributes. When
a CROSS JOIN is achieved between R1 and R2,
how many tuples would the resultant set have?
A. 28
B. 10
C. 0
D. 35
MCQs
Relation R1 has 10 tuples and 5 attributes.
Relation R2 has 0 tuples and 7 attributes. When
a CROSS JOIN is achieved between R1 and R2,
how many tuples would the resultant set have?
A. 28
B. 10
C. 0
D. 35
MCQs
The ___________ operation, denoted by −,
allows us to find tuples that are in one relation
but are not in another.
a) Union
b) Set-difference
c) Difference
d) Intersection
MCQs
The ___________ operation, denoted by −,
allows us to find tuples that are in one relation
but are not in another.
a) Union
b) Set-difference
c) Difference
d) Intersection