Unit 3
Unit 3
1
Relational Algebra
in DBMS
2
Introduction of RELATIONAL ALGEBRA …
• RELATIONAL ALGEBRA is a widely used
procedural query language.
• RELATIONAL ALGEBRA is a language for
expressing relation database queries.
3
Relational Algebra divided in various groups
Unary Relational Operations
SELECT (symbol: σ)
PROJECT (symbol: π)
RENAME (symbol: ρ)
Relational Algebra Operations From Set Theory
UNION (υ)
INTERSECTION (∩)
DIFFERENCE (-)
CARTESIAN PRODUCT ( x )
Binary Relational Operations
JOIN
DIVISION
4
Selection Operator
🞂Symbol: σ (Sigma)
🞂Notation: σ condition (Relation)
🞂Operation: Selects tuples from a relation
that satisfy a given condition.
🞂Operators: =, <>, <, >, <=, >=, Λ (AND),
V (OR)
5
Display the detail of students belongs to “CE”
Branch.
Student RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh Cl 9
104 Meet CE 9
6
σBranch=‘CE’ (Student)
7
Display the detail of students belongs
to “CE” Branch and having SPI more
than 8.
8
Display the detail of students belongs to either
“CI” or “ME” Branch.
σBranch=‘Cl’ V Branch=‘ME’ (Student)
Output
9
Display the detail of students whose SPI
between 7 and 9.
σSPI>7 Λ SPI<9 (Student)
10
Relational Algebra Operations
Projection Operator
11
Projection Operator
🞂 Symbol: ∏ (Pi)
🞂 Notation: ∏ attribute set (Relation)
🞂 Operation: Selects specified attributes of a relation.
🞂 It removes duplicate tuples (records) from the result.
12
Display RollNo, Name and Branch of all
students.
∏ RollNo, Name, Branch (Student)
Output RollNo Name Branch
101 Raju CE
102 Mitesh ME
103 Nilesh Cl
104 Meet CE
13
Display Name and SPI of all students.
Name SPI
Raju 8
Mitesh 9
Nilesh 9
Meet 9
14
Combined Projection & Selection Operation
Student
Roll.N Name Branc SPI
o h
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh Cl 9
104 Meet CE 7
15
Display RollNo, Name & Branch of “ME”
Branch students.
σBranch=‘ME’ (Student)
Roll.N Name Branc SPI
o h
102 Mitesh ME 9
16
Display Name, Branch and SPI of students
whose SPI is more than 8.
σSPI>8 (Student)
∏ Name, Branch, SPI (σSPI>8 (Student))
Roll.N Name Branc SPI Name Branc SPI
o h h
102 Mitesh ME 9 Mitesh ME 9
17
Display Name, Branch and SPI of students who
belongs to “CE” Branch and SPI is more than 7.
σBranch=‘CE’ Λ SPI>7
(Student)
18
Display Name, Branch and SPI of students who
belongs to “CE” Branch and SPI is more than 7.
19
Display Name of students along with their
Branch who belong to either “ME” Branch or
“CI” Branch.
20
Display Name of students along with their
Branch who belong to either “ME” Branch or
“CI” Branch.
Nilesh Cl
21
Types of set
22
Union Operator
• Symbol: U
• Notation: Relation-1 (R1) U Relation-2 (R2) OR Algebra-1
U Algebra-2
23
Intersect/ Intersection Operator
Symbol: ∩
Notation: Relation-1 (R1) ∩ Relation-2 (R2) OR Algebra-1
∩ Algebra-2
It displays all the tuples/records which are common from both
relations.
Amit Amit
Customer Employee Customer
Amit ∩
Dip
Employee Punit Jay
Jay
Jay
24
Minus/ Set difference Operator
Symbol: −
Notation: Relation-1 (R1) − Relation-2 (R2) OR Algebra-1
− Algebra-2
25
Union Operators Example
Display Name of person who are either employee or
customer. ∏ (Customer) U ∏
Name Name
(Employee)
Raju
Suresh
Manoj
26
Intersect/ Intersection Operators Example
Display Name of person who are employee as well as
customer. ∏ Name (Customer) ∩ ∏ Name (Employee)
Suresh
27
Minus/ Set difference Operators Example
Display Name of person who are employee but not
customer. ∏ Name (Employee) - ∏ Name (Customer)
Manoj
28
Cartesian Product(✕)
Notation: S ✕ R
where A and S are the relations,
the symbol ‘✕’ is used to denote the CROSS
PRODUCT operator. Consider two relations
STUDENT(SNO, First NAME, Last NAME) and
DETAIL(ROLLNO, AGE) below:
Sr.No First Name Last Name RollNo Age
1 Amit Vyas 5 25
2 Punit Patel 9 28
29
output
Sr.No First Last RollNo Age
Name Name
1 Amit Vyas 5 25
1 Amit Vyas 9 28
2 Punit Patel 5 25
2 Punit Patel 9 28
30
Division Operator
🞂 Symbol: ÷ (Division)
🞂 Notation: Relation1 (R1) ÷ Relation2 (R2) OR Algebra1 ÷
Algebra2
31
Relational Algebra Operations
Division Operator
STUDENT SUBJECT (STUDENT/
SUBJECT)
NAME SUBJE SUBJE
Name
CT CT
ROHIT
AMIT DBMS DBMS
AMIT DS DS
DIP DS
DIP DBMS
ROHIT DS
32
Rename Operator
🞂 Symbol: ρ (Rho)
🞂 Notation: ρA (X1,X2….Xn) (Relation)
🞂 Operation:
The rename operation is used to rename the output relation.
The result of rename operator are also relations with new
name.
The original relation name can not be changed when we
perform rename operation on any relation.
ρ A1, A2. …,An (E)
Returns a relation E with the attributes renamed to A1, A2,
…., An.
33
Rename table
Student
RN
Name CPI
o
101 Raj 8
102 Meet 9
103 Jay 7
34
Person
ρPerson (Student)
RN
Name CPI
o
101 Raj 8
102 Meet 9
103 Jay 7
35
ρ(RollNo, StudentName, CPI) (Student)
RollN Student
CPI
o Name
101 Raj 8
102 Meet 9
103 Jay 7
36
Rename table and attributes both
student
101 Raj 8
102 Meet 9
103 Jay 7
37
ρPerson (ρ(RollNo, StudentName) (∏ RNo, Name (Student)))
Person
RollNo StudentName
101 Raj
102 Meet
103 Jay
38
Aggregate Functions
RNo Name CPI
101 Raj 8
102 Meet 9
103 Jay 7
39
Find out sum of CPI of all students.
g sum(CPI) (Student)
sum
24
40
Find out maximum & minimum CPI.
g max(CPI), min(CPI) (Student)
max min
9 7
41
Count the number of students.
g count(Rno) (Student)
Count
3
42
Find out average of CPI of all students.
g avg(CPI) (Student)
Avg
8
43
Relational Algebra Operations
Natural Join / Inner Join
44
Natural Join
Notation: R1 R2
Where:
The selection s checks equality of all common attributes
C
The projection eliminates the duplicate common attributes
45
Natural Join Example
Employee
Name SSN
John 999999999
Tony 777777777
Dependents
SSN Dname
999999999 Emily
777777777 Joe
Employee Dependents =
PName, SSN, Dname(s SSN=SSN2(Employee x rSSN2, Dname(Dependents))
46
Open Source DBMS Commercial DBMS
DBMS, which is available in the market at free of DBMS, which is available in the market at a
cost. certain price.
The code of open source DBMS product can be The code of commercial DBMS product can not
viewed, shared or modified by the community. be view, share or modify by the community.
There are chances of malfunctioning with code The security is high and code is not accessible to
as source code is open. unauthorized person.
Examples: MySQL, MongoDB, SQLite etc Examples: Microsoft SQL Server, IBM Db2 etc
47
Write down relational algebra for the following
tables/relations
Student (Rno, Sname, Address, City, Mobile)
Department (Did, Dname)
Academic (Rno, Did, SPI, CPI, Backlog)
Guide (Rno, PName, Fid)
Faculty (Fid, Fname, Subject, Did, Salary)
List the name of students with their department name
and SPI of all student belong to “CE” department.
∏ Sname, Dname, SPI (σDname=‘CE’ (Student (Department
Academic)))
48
Display the name of students with their
project name whose guide is “A.J.Shah”.
∏ Sname, Pname (σFname=‘A.J.Shah’ (Student (Guide
Faculty)))
49
Consider following schema and represent given
statements in relation algebra form.
Branch(branch_name,branch_city)
Account(branch_name, acc_no, balance)
Depositor(customer_name, acc_no)
50
Find out list of customer who have account at
‘abc’ branch.
51
Find out all customer who have an account in
‘Ahmedabad’ city and balance is greater than
10,000.
53
Thank You
54