SlideShare a Scribd company logo
1 of 56
Course – DBMS
Course Instructor
Dr. Vinod Kumar
8 September 2022 CSE, KL University
1
Instruction to Students
Apart from the contents presented in the slides students should
read through the text book also.
8 September 2022 CSE, KL University 2
Topics Covered in Todays Class
Unit 2:
Introduction to Relational Algebra
8 September 2022 CSE, KL University 3
Why you should Learn “Relational Algebra” ?
8 September 2022 CSE, KL University 4
Why you should Learn “Relational Algebra” ?
Relational Algebra is
• Core of Relational Query Language, Example: SQL
• Provides framework for Query implementation and optimization
• Is a mathematical language for manipulating relations.
8 September 2022 CSE, KL University 5
Relational Algebra based on Relational Model
8 September 2022 CSE, KL University 6
USN Name
1BM14CS001 Arjun
1BM14CS002 Balaji
student_info
Relational model due to Edgar Teds Codd,
a mathematician at IBM in 1970
Won Turing
award 1981
Relational Algebra based on Relational Model
8 September 2022 CSE, KL University 7
Row
or
Tuple
or
Record
Database table Name: student_info
USN Name
1BM14CS001 Arjun
1BM14CS002 Balaji
Column or Attribute
Table
or
Relation
USN Name
1BM14CS001 Arjun
1BM14CS002 Balaji
student_info
Relational model due to Edgar “Ted” Codd,
a mathematician at IBM in 1970
Won Turing
award 1981
RDBMS Architecture
8 September 2022 CSE, KL University 8
Relational Database Management System (RDBMS)
How does a SQL engine work ?
SQL
Query
Relational
Algebra (RA)
Plan
Optimized
RA Plan
Execution
Declarative query
(from user)
Translate to relational
algebra expression
Find logically
equivalent- but more
efficient- RA
expression
Execute each
operator of the
optimized plan!
RDBMS Architecture
8 September 2022 CSE, KL University 9
Relational Database Management System (RDBMS)
How does a SQL engine work ?
Relational Algebra allows us to translate declarative (SQL) queries
into precise and optimizable expressions!
What is an “Algebra”
• Mathematical system consisting of:
– Operands: variables or values from which new values can be
constructed.
– Operators: symbols denoting procedures that construct new values
from given values.
8 September 2022 CSE, KL University 10
What is Relational Algebra (RA) ?
• An algebra whose operands are database tables or relations or variables
that represent relations.
• Operators are designed to do the most common things that we need to do
with relations in a database.
– The result is an algebra that can be used as a query language for relations.
8 September 2022 CSE, KL University 11
Relational Algebra Operations
Keep in mind: RA operates on sets!
• RDBMSs use multisets, however in relational algebra formalism we will
consider sets!
• Also: we will consider the named perspective, where every attribute must
have a unique name
– attribute order does not matter…
8 September 2022 CSE, KL University 12
Unary Relational Operations
• Select Operation (σ sigma)
• Project Operation (∏ phi)
8 September 2022 CSE, KL University 13
Select Operation (σ sigma)
• It selects tuples that satisfy the given predicate from a relation.
• Select written as
Notation σselection condition(R)
• Where σ stands for selection predicate
• R stands for relation / table name
• Selection condition is prepositional logic formula which may use
connectors like and, or, and not. These terms may use relational operators
like − =, ≠, ≥, < , >, ≤.
8 September 2022 CSE, KL University 14
Select Operation (σ)
8 September 2022 CSE, KL University 15
Select Operation (σ)
Returns all tuples which satisfy a condition
SELECT *
FROM Student
WHERE marks > 60;
SQL:
σselection condition(R)
Student
Equivalent
Relational Algebra
Expression
Select Operation (σ)
8 September 2022 CSE, KL University 16
Select Operation (σ)
Returns all tuples which satisfy a condition
SELECT *
FROM Student
WHERE marks > 60;
SQL:
Relational Algebra Expression
σselection condition(R)
Student
Select Operation (σ)
8 September 2022 CSE, KL University 17
Select Operation (σ)
Returns all tuples which satisfy a condition
SELECT *
FROM Student
WHERE name=‘Avinash’;
SQL:
σselection condition(R)
Student
Relational Algebra Expression
Project Operation (∏ phi)
• It projects column(s) that satisfy a given predicate.
• Written as
Notation ∏A1,…., An (R)
• Where A1, A2 , An are attribute names of relation R.
• Duplicate rows are automatically eliminated, as relation is a
set.
8 September 2022 CSE, KL University 18
Project Operation (∏ phi)
• Eliminates columns, then removes duplicates
• Written as
Notation ∏A1,…., An (R)
8 September 2022 CSE, KL University 19
SELECT DISTINCT
dep_num
FROM Student;
SQL:
Student
dep_nu
m
10
20
Relational Algebra Expression
Project Operation (∏ phi)
• Eliminates columns, then removes duplicates
• Written as
Notation ∏A1,…., An (R)
8 September 2022 CSE, KL University 20
SELECT DISTINCT
name, dep_num
FROM Student;
SQL:
Student
Relational Algebra Expression
Note that RA Operators are Compositional!
8 September 2022 CSE, KL University 21
SELECT DISTINCT
name, marks
FROM Student
WHERE marks > 60;
How do we represent this query in
Relational Algebra?
Student
Note that RA Operators are Compositional!
8 September 2022 CSE, KL University 22
SELECT DISTINCT
name, marks
FROM Student
WHERE marks > 60;
How do we represent this query in
Relational Algebra?
Student
Note that RA Operators are Compositional!
8 September 2022 CSE, KL University 23
SELECT DISTINCT
name, marks
FROM Student
WHERE marks > 60;
Are these logically equivalent?
Student
How do we represent this query in
Relational Algebra?
Note that RA Operators are Compositional!
8 September 2022 CSE, KL University 24
SELECT DISTINCT
name, marks
FROM Student
WHERE marks > 60;
Are these logically equivalent?
Student
How do we represent this query in
Relational Algebra?
Question
8 September 2022 CSE, KL University 25
SELECT DISTINCT
name, marks
FROM Student
WHERE marks > 60;
Student
What is the output of following
relational algebra query ?
Question
8 September 2022 CSE, KL University 26
SELECT DISTINCT
name, marks
FROM Student
WHERE marks > 60;
Student
What is the output of following
relational algebra query ?
Question
8 September 2022 CSE, KL University 27
SELECT DISTINCT
usn, name, marks
FROM Student
WHERE marks > 60;
Student
What is the output of following
relational algebra query ?
Question
8 September 2022 CSE, KL University 28
SELECT DISTINCT
usn, name, marks
FROM Student
WHERE marks > 60;
Student
What is the output of following
relational algebra query ?
Question
• Consider the relational schema Schedule containing Theater name, Movie
Title and timing at which movie will be played. Time in the Schedule table
is stored in 24Hr format i.e., 6:00pm will be stored as 18:00
Schedule(Theater, MovieTitle, Time)
Write the following query both in Relational Algebra and SQL.
List the theater name and time where we can watch the movie “FndingNemo" after
3pm.
8 September 2022 CSE, KL University 29
Question
• Consider the relational schema Schedule containing Theater name, Movie
Title and timing at which movie will be played. Time in the Schedule table
is stored in 24Hr format i.e., 6:00pm will be stored as 18:00
Schedule(Theater, MovieTitle, Time)
Write the following query both in Relational Algebra and SQL.
List the theater name and time where we can watch the movie “FndingNemo" after
3pm.
8 September 2022 CSE, KL University 30
SELECT Theater, Time
FROM Schedule
WHERE MovieTitle = ’FindingNemo’ AND time >= 15:00;
Database Philosophy
8 September 2022 CSE, KL University 31
Won Turing
award 1981
Edgar Ted Codd
Rename Operation (ρ rho)
• The rename operation allows us to rename the either relation
name or attribute names or both.
• Written as
Notation
ρS(B1,…., Bn) (R) or ρS(R) or ρ(B1,…., Bn) (R)
• S is the new relation name and B1, B2….Bn are new attribute
names.
8 September 2022 CSE, KL University 32
Rename Operation (ρ rho)
• The rename operation allows us to rename the either relation name or attribute names or
both.
8 September 2022 CSE, KL University 33
SELECT
usn as Student_USN
name as Student_Name
dep_num as Department_Number
FROM Student;
SQL:
Student
ρStudent_USN, Student_Name, Department_Number (Student)
Student
Binary Operations
8 September 2022 CSE, KL University 34
• The CARTESIAN PRODUCT or CROSS JOIN returns the Cartesian product of the sets of
records from the two or more joined tables.
• Notation: table1  table2
8 September 2022 CSE, KL University 35
ID M
1 a
2 b
4 c
table1
ID N
2 p
3 q
5 r
table2
table1 x table2
Relational Algebra Expression
8 September 2022 CSE, KL University 36
Join Operation
Written as R ⋈joincondition S
Emp_ID D_ID
121 2
240 4
Emp_Dept
D_num D_name
1 Sale
2 Account
4 Marketing
Dept
Emp_ID D_ID D_num D_name
121 2 2 Account
240 4 4 Marketing
Emp_Dept ⋈D_ID=D_num Dept
• Join Written as R ⋈joincondition S
• JOIN operation is used to combine related records from two tables into a single
records
• A general join condition is of the form
<condition> AND <condition> AND……AND <condition>
• Where each condition is of the form Ai θ Bj , Ai is an attribute in relation R and Bj is
an attribute in relation S, Ai and Bj have same domain
– It is called a theta join, if θ is one of the comparison operators {< , > , = , ≠ , ≤ , ≥}.
– Once again, if θ is = it's called an equijoin, and
– if the equijoin is on same-named attributes it's called a natural join and written as *.
• Similarly, there are left, right, and full outer joins; written as ⟕, ⟖, and
⟗ respectively.
8 September 2022 CSE, KL University 37
Join Operations
8 September 2022 CSE, KL University 38
Join
Inner Join
Natural Join(*) (common attribute
names and domain
should exist between
two tables)
Outer Join
Left Outer Join ⟕
Right Outer Join ⟖
Full Outer Join ⟗
Equi Join
8 September 2022 CSE, KL University 39
num M
1 a
2 b
4 c
table1
ID N
2 p
3 q
5 r
table2
num M ID N
2 b 2 p
Select *
From table1, table 2
Where num=id;
What is the equivalent
relational algebra expression ?
Equi Join
8 September 2022 CSE, KL University 40
num M
1 a
2 b
4 c
table1
ID N
2 p
3 q
5 r
table2
num M ID N
2 b 2 p
Select *
From table1, table 2
Where num=id;
num M ID N
2 b 2 p
result
Question
8 September 2022 CSE, KL University 41
Are these logically equivalent?
num M
1 a
2 b
4 c
table1
ID N
2 p
3 q
5 r
table2
num M ID N
2 b 2 p
result
Natural Join
8 September 2022 CSE, KL University 42
ID M
1 a
2 b
4 c
table1
ID N
2 p
3 q
5 r
table2
ID M N
2 b p
result
Question
8 September 2022 CSE, KL University 43
Are these logically equivalent?
ID M
1 a
2 b
4 c
table1
ID N
2 p
3 q
5 r
table2
ID M N
2 b p
result
What will be the output of following Relational Algebra
Expression
8 September 2022 CSE, KL University 44
What will be the output of following Relational Algebra
Expression
8 September 2022 CSE, KL University 45
Natural Join
8 September 2022 CSE, KL University 46
ID M
1 a
2 b
4 c
table1
ID N
2 p
3 q
5 r
table2
ID M N
2 b p
result
Natural join does not use any comparison operator. It does not
concatenate the way a Cartesian product does. We can perform a
Natural Join only if there is at least one common attribute that exists
between two relations. In addition, the attributes must have the same
name and domain. Natural join acts on those matching attributes
where the values of attributes in both the relations are same.
Problem to solve
Consider three tables
• SAILORS(Sal_ID, SalName, Rating, Age)
• RESERVES(Sal_ID , Boat-ID, Rdate)
• BOATS(Boat-ID, BoatName, Color)
Write Relational Algebra express for the following
i. Find all the names of Sailors who have reserved boat with ID 2
ii. Find names of sialors who have reserved RED boat
iii. Find the colors of the boat reserved by Avinash
8 September 2022 CSE, KL University 47
Problem to solve
8 September 2022 CSE, KL University 48
Problem to solve
8 September 2022 CSE, KL University 49
Consider three tables
SAILORS(Sal_ID, SalName, Rating, Age)
RESERVES(Sal_ID , Boat-ID, Rdate)
BOATS(Boat-ID, BoatName, Color)
Write Relational Algebra express for the following
i. Find all the names of Sailors who have reserved boat with ID 2
OUTPUT
--------
Balaji
Dinesh
Problem to solve
8 September 2022 CSE, KL University 50
Consider three tables
SAILORS(Sal_ID, SalName, Rating, Age)
RESERVES(Sal_ID , Boat-ID, Rdate)
BOATS(Boat-ID, BoatName, Color)
Write Relational Algebra express for the following
i. Find all the names of Sailors who have reserved boat with ID 2
temp1 <- σBoat_ID=2
(RESERVES) SELECT *
FROM RESERVES
WHERE Boat_ID = 2;
temp1
Problem to solve
8 September 2022 CSE, KL University 51
Consider three tables
SAILORS(Sal_ID, SalName, Rating, Age)
RESERVES(Sal_ID , Boat-ID, Rdate)
BOATS(Boat-ID, BoatName, Color)
Write Relational Algebra express for the following
i. Find all the names of Sailors who have reserved boat with ID 2
temp1 <- σBoat_ID=2
(RESERVES)
temp2 <- 𝞹SAL_ID(temp1)
SELECT SAIL_ID
FROM RESERVES
WHERE Boat_ID = 2;
temp2
Problem to solve
8 September 2022 CSE, KL University 52
i. Find all the names of Sailors who have reserved boat with ID 2
temp1 <- σBoat_ID=2
(RESERVES)
temp2 <- 𝞹SAL_ID(temp1)
temp3 <- SAILORS ⋈ SAILORS.Sal_ID=temp2.Sal_ID (temp2)
SELECT *
FROM SAILORS
NATURAL JOIN
(SELECT SAL_ID
FROM RESERVES
WHERE Boat_ID = 2);
temp3
Problem to solve
8 September 2022 CSE, KL University 53
i. Find all the names of Sailors who have reserved boat with ID 2
temp1 <- σBoat_ID=2
(RESERVES)
temp2 <- 𝞹SAL_ID(temp1)
temp3 <- SAILORS ⋈ SAILORS.Sal_ID=temp2.Sal_ID (temp2)
result <- 𝞹SAL_Name
(temp3)
SELECT SalName
FROM SAILORS
NATURAL JOIN
(SELECT SAL_ID
FROM RESERVES
WHERE Boat_ID = 2);
result
Problem to solve
8 September 2022 CSE, KL University 54
i. Find all the names of Sailors who have reserved boat with ID 2
temp1 <- σBoat_ID=2
(RESERVES)
temp2 <- 𝞹SAL_ID(temp1)
temp3 <- SAILORS ⋈ SAILORS.Sal_ID=temp2.Sal_ID (temp2)
result <- 𝞹SAL_Name
(temp3)
SAILORS(Sal_ID, SalName, Rating, Age), RESERVES(Sal_ID , Boat-ID, Rdate),
BOATS(Boat-ID, BoatName, Color)
8 September 2022 CSE, KL University 55
Write Relational Algebra express for the following
ii. Find names of sailors who have reserved RED boat
OUTPUT
--------
Balaji
Dinesh
SAILORS(Sal_ID, SalName, Rating, Age), RESERVES(Sal_ID , Boat-ID, Rdate),
BOATS(Boat-ID, BoatName, Color)
8 September 2022 CSE, KL University 56
Write Relational Algebra express for the following
ii. Find the colors of the boat reserved by Avinash
OUTPUT
--------
Red

More Related Content

What's hot

Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03
Jotham Gadot
 
3. Relational Models in DBMS
3. Relational Models in DBMS3. Relational Models in DBMS
3. Relational Models in DBMS
koolkampus
 
3 data modeling using the entity-relationship (er) model
3 data modeling using the entity-relationship (er) model3 data modeling using the entity-relationship (er) model
3 data modeling using the entity-relationship (er) model
Kumar
 
Relations
RelationsRelations
Relations
Gaditek
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
shekhar1991
 

What's hot (20)

Chapter-1 Introduction to Database Management Systems
Chapter-1 Introduction to Database Management SystemsChapter-1 Introduction to Database Management Systems
Chapter-1 Introduction to Database Management Systems
 
Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03
 
3. Relational Models in DBMS
3. Relational Models in DBMS3. Relational Models in DBMS
3. Relational Models in DBMS
 
Object oriented database
Object oriented databaseObject oriented database
Object oriented database
 
Relational algebra.pptx
Relational algebra.pptxRelational algebra.pptx
Relational algebra.pptx
 
Unit 1: Introduction to DBMS Unit 1 Complete
Unit 1: Introduction to DBMS Unit 1 CompleteUnit 1: Introduction to DBMS Unit 1 Complete
Unit 1: Introduction to DBMS Unit 1 Complete
 
3 data modeling using the entity-relationship (er) model
3 data modeling using the entity-relationship (er) model3 data modeling using the entity-relationship (er) model
3 data modeling using the entity-relationship (er) model
 
Introduction of DBMS
Introduction of DBMSIntroduction of DBMS
Introduction of DBMS
 
MENGELOLA SUMBER DAYA DATA PERUSAHAAN.pptx
MENGELOLA SUMBER DAYA DATA PERUSAHAAN.pptxMENGELOLA SUMBER DAYA DATA PERUSAHAAN.pptx
MENGELOLA SUMBER DAYA DATA PERUSAHAAN.pptx
 
Unit-3_BDA.ppt
Unit-3_BDA.pptUnit-3_BDA.ppt
Unit-3_BDA.ppt
 
Graph based data models
Graph based data modelsGraph based data models
Graph based data models
 
Dbms 11: Relational Algebra
Dbms 11: Relational AlgebraDbms 11: Relational Algebra
Dbms 11: Relational Algebra
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculus
 
Relations
RelationsRelations
Relations
 
Xml databases
Xml databasesXml databases
Xml databases
 
Chap05 c
Chap05 cChap05 c
Chap05 c
 
Graph database
Graph database Graph database
Graph database
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Data Exploration and Visualization with R
Data Exploration and Visualization with RData Exploration and Visualization with R
Data Exploration and Visualization with R
 
Data manipulation language
Data manipulation languageData manipulation language
Data manipulation language
 

Similar to Relational Algebra1.pptx

Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
emailharmeet
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
meharikiros2
 

Similar to Relational Algebra1.pptx (20)

Unit3_DIS.ppt
Unit3_DIS.pptUnit3_DIS.ppt
Unit3_DIS.ppt
 
Introduction to Relational Database Management Systems
Introduction to Relational Database Management SystemsIntroduction to Relational Database Management Systems
Introduction to Relational Database Management Systems
 
2 rel-algebra
2 rel-algebra2 rel-algebra
2 rel-algebra
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
 
uniT 4 (1).pptx
uniT 4 (1).pptxuniT 4 (1).pptx
uniT 4 (1).pptx
 
Relational Algebra and it's Operations pptx
Relational Algebra and it's Operations pptxRelational Algebra and it's Operations pptx
Relational Algebra and it's Operations pptx
 
R Algebra.ppt
R Algebra.pptR Algebra.ppt
R Algebra.ppt
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
ch2
ch2ch2
ch2
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
 
Java Database Connectivity with JDBC.pptx
Java Database Connectivity with JDBC.pptxJava Database Connectivity with JDBC.pptx
Java Database Connectivity with JDBC.pptx
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational Algebra
 
chapter 5-Relational Algebra and calculus.ppt
chapter 5-Relational Algebra and calculus.pptchapter 5-Relational Algebra and calculus.ppt
chapter 5-Relational Algebra and calculus.ppt
 
Lecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxLecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptx
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
MODULE-2dbmsmodule2chapter1isedepts.pptx
MODULE-2dbmsmodule2chapter1isedepts.pptxMODULE-2dbmsmodule2chapter1isedepts.pptx
MODULE-2dbmsmodule2chapter1isedepts.pptx
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
Relational Model
Relational ModelRelational Model
Relational Model
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Relational Algebra1.pptx

  • 1. Course – DBMS Course Instructor Dr. Vinod Kumar 8 September 2022 CSE, KL University 1
  • 2. Instruction to Students Apart from the contents presented in the slides students should read through the text book also. 8 September 2022 CSE, KL University 2
  • 3. Topics Covered in Todays Class Unit 2: Introduction to Relational Algebra 8 September 2022 CSE, KL University 3
  • 4. Why you should Learn “Relational Algebra” ? 8 September 2022 CSE, KL University 4
  • 5. Why you should Learn “Relational Algebra” ? Relational Algebra is • Core of Relational Query Language, Example: SQL • Provides framework for Query implementation and optimization • Is a mathematical language for manipulating relations. 8 September 2022 CSE, KL University 5
  • 6. Relational Algebra based on Relational Model 8 September 2022 CSE, KL University 6 USN Name 1BM14CS001 Arjun 1BM14CS002 Balaji student_info Relational model due to Edgar Teds Codd, a mathematician at IBM in 1970 Won Turing award 1981
  • 7. Relational Algebra based on Relational Model 8 September 2022 CSE, KL University 7 Row or Tuple or Record Database table Name: student_info USN Name 1BM14CS001 Arjun 1BM14CS002 Balaji Column or Attribute Table or Relation USN Name 1BM14CS001 Arjun 1BM14CS002 Balaji student_info Relational model due to Edgar “Ted” Codd, a mathematician at IBM in 1970 Won Turing award 1981
  • 8. RDBMS Architecture 8 September 2022 CSE, KL University 8 Relational Database Management System (RDBMS) How does a SQL engine work ? SQL Query Relational Algebra (RA) Plan Optimized RA Plan Execution Declarative query (from user) Translate to relational algebra expression Find logically equivalent- but more efficient- RA expression Execute each operator of the optimized plan!
  • 9. RDBMS Architecture 8 September 2022 CSE, KL University 9 Relational Database Management System (RDBMS) How does a SQL engine work ? Relational Algebra allows us to translate declarative (SQL) queries into precise and optimizable expressions!
  • 10. What is an “Algebra” • Mathematical system consisting of: – Operands: variables or values from which new values can be constructed. – Operators: symbols denoting procedures that construct new values from given values. 8 September 2022 CSE, KL University 10
  • 11. What is Relational Algebra (RA) ? • An algebra whose operands are database tables or relations or variables that represent relations. • Operators are designed to do the most common things that we need to do with relations in a database. – The result is an algebra that can be used as a query language for relations. 8 September 2022 CSE, KL University 11 Relational Algebra Operations
  • 12. Keep in mind: RA operates on sets! • RDBMSs use multisets, however in relational algebra formalism we will consider sets! • Also: we will consider the named perspective, where every attribute must have a unique name – attribute order does not matter… 8 September 2022 CSE, KL University 12
  • 13. Unary Relational Operations • Select Operation (σ sigma) • Project Operation (∏ phi) 8 September 2022 CSE, KL University 13
  • 14. Select Operation (σ sigma) • It selects tuples that satisfy the given predicate from a relation. • Select written as Notation σselection condition(R) • Where σ stands for selection predicate • R stands for relation / table name • Selection condition is prepositional logic formula which may use connectors like and, or, and not. These terms may use relational operators like − =, ≠, ≥, < , >, ≤. 8 September 2022 CSE, KL University 14
  • 15. Select Operation (σ) 8 September 2022 CSE, KL University 15 Select Operation (σ) Returns all tuples which satisfy a condition SELECT * FROM Student WHERE marks > 60; SQL: σselection condition(R) Student Equivalent Relational Algebra Expression
  • 16. Select Operation (σ) 8 September 2022 CSE, KL University 16 Select Operation (σ) Returns all tuples which satisfy a condition SELECT * FROM Student WHERE marks > 60; SQL: Relational Algebra Expression σselection condition(R) Student
  • 17. Select Operation (σ) 8 September 2022 CSE, KL University 17 Select Operation (σ) Returns all tuples which satisfy a condition SELECT * FROM Student WHERE name=‘Avinash’; SQL: σselection condition(R) Student Relational Algebra Expression
  • 18. Project Operation (∏ phi) • It projects column(s) that satisfy a given predicate. • Written as Notation ∏A1,…., An (R) • Where A1, A2 , An are attribute names of relation R. • Duplicate rows are automatically eliminated, as relation is a set. 8 September 2022 CSE, KL University 18
  • 19. Project Operation (∏ phi) • Eliminates columns, then removes duplicates • Written as Notation ∏A1,…., An (R) 8 September 2022 CSE, KL University 19 SELECT DISTINCT dep_num FROM Student; SQL: Student dep_nu m 10 20 Relational Algebra Expression
  • 20. Project Operation (∏ phi) • Eliminates columns, then removes duplicates • Written as Notation ∏A1,…., An (R) 8 September 2022 CSE, KL University 20 SELECT DISTINCT name, dep_num FROM Student; SQL: Student Relational Algebra Expression
  • 21. Note that RA Operators are Compositional! 8 September 2022 CSE, KL University 21 SELECT DISTINCT name, marks FROM Student WHERE marks > 60; How do we represent this query in Relational Algebra? Student
  • 22. Note that RA Operators are Compositional! 8 September 2022 CSE, KL University 22 SELECT DISTINCT name, marks FROM Student WHERE marks > 60; How do we represent this query in Relational Algebra? Student
  • 23. Note that RA Operators are Compositional! 8 September 2022 CSE, KL University 23 SELECT DISTINCT name, marks FROM Student WHERE marks > 60; Are these logically equivalent? Student How do we represent this query in Relational Algebra?
  • 24. Note that RA Operators are Compositional! 8 September 2022 CSE, KL University 24 SELECT DISTINCT name, marks FROM Student WHERE marks > 60; Are these logically equivalent? Student How do we represent this query in Relational Algebra?
  • 25. Question 8 September 2022 CSE, KL University 25 SELECT DISTINCT name, marks FROM Student WHERE marks > 60; Student What is the output of following relational algebra query ?
  • 26. Question 8 September 2022 CSE, KL University 26 SELECT DISTINCT name, marks FROM Student WHERE marks > 60; Student What is the output of following relational algebra query ?
  • 27. Question 8 September 2022 CSE, KL University 27 SELECT DISTINCT usn, name, marks FROM Student WHERE marks > 60; Student What is the output of following relational algebra query ?
  • 28. Question 8 September 2022 CSE, KL University 28 SELECT DISTINCT usn, name, marks FROM Student WHERE marks > 60; Student What is the output of following relational algebra query ?
  • 29. Question • Consider the relational schema Schedule containing Theater name, Movie Title and timing at which movie will be played. Time in the Schedule table is stored in 24Hr format i.e., 6:00pm will be stored as 18:00 Schedule(Theater, MovieTitle, Time) Write the following query both in Relational Algebra and SQL. List the theater name and time where we can watch the movie “FndingNemo" after 3pm. 8 September 2022 CSE, KL University 29
  • 30. Question • Consider the relational schema Schedule containing Theater name, Movie Title and timing at which movie will be played. Time in the Schedule table is stored in 24Hr format i.e., 6:00pm will be stored as 18:00 Schedule(Theater, MovieTitle, Time) Write the following query both in Relational Algebra and SQL. List the theater name and time where we can watch the movie “FndingNemo" after 3pm. 8 September 2022 CSE, KL University 30 SELECT Theater, Time FROM Schedule WHERE MovieTitle = ’FindingNemo’ AND time >= 15:00;
  • 31. Database Philosophy 8 September 2022 CSE, KL University 31 Won Turing award 1981 Edgar Ted Codd
  • 32. Rename Operation (ρ rho) • The rename operation allows us to rename the either relation name or attribute names or both. • Written as Notation ρS(B1,…., Bn) (R) or ρS(R) or ρ(B1,…., Bn) (R) • S is the new relation name and B1, B2….Bn are new attribute names. 8 September 2022 CSE, KL University 32
  • 33. Rename Operation (ρ rho) • The rename operation allows us to rename the either relation name or attribute names or both. 8 September 2022 CSE, KL University 33 SELECT usn as Student_USN name as Student_Name dep_num as Department_Number FROM Student; SQL: Student ρStudent_USN, Student_Name, Department_Number (Student) Student
  • 34. Binary Operations 8 September 2022 CSE, KL University 34
  • 35. • The CARTESIAN PRODUCT or CROSS JOIN returns the Cartesian product of the sets of records from the two or more joined tables. • Notation: table1  table2 8 September 2022 CSE, KL University 35 ID M 1 a 2 b 4 c table1 ID N 2 p 3 q 5 r table2 table1 x table2 Relational Algebra Expression
  • 36. 8 September 2022 CSE, KL University 36 Join Operation Written as R ⋈joincondition S Emp_ID D_ID 121 2 240 4 Emp_Dept D_num D_name 1 Sale 2 Account 4 Marketing Dept Emp_ID D_ID D_num D_name 121 2 2 Account 240 4 4 Marketing Emp_Dept ⋈D_ID=D_num Dept
  • 37. • Join Written as R ⋈joincondition S • JOIN operation is used to combine related records from two tables into a single records • A general join condition is of the form <condition> AND <condition> AND……AND <condition> • Where each condition is of the form Ai θ Bj , Ai is an attribute in relation R and Bj is an attribute in relation S, Ai and Bj have same domain – It is called a theta join, if θ is one of the comparison operators {< , > , = , ≠ , ≤ , ≥}. – Once again, if θ is = it's called an equijoin, and – if the equijoin is on same-named attributes it's called a natural join and written as *. • Similarly, there are left, right, and full outer joins; written as ⟕, ⟖, and ⟗ respectively. 8 September 2022 CSE, KL University 37
  • 38. Join Operations 8 September 2022 CSE, KL University 38 Join Inner Join Natural Join(*) (common attribute names and domain should exist between two tables) Outer Join Left Outer Join ⟕ Right Outer Join ⟖ Full Outer Join ⟗
  • 39. Equi Join 8 September 2022 CSE, KL University 39 num M 1 a 2 b 4 c table1 ID N 2 p 3 q 5 r table2 num M ID N 2 b 2 p Select * From table1, table 2 Where num=id; What is the equivalent relational algebra expression ?
  • 40. Equi Join 8 September 2022 CSE, KL University 40 num M 1 a 2 b 4 c table1 ID N 2 p 3 q 5 r table2 num M ID N 2 b 2 p Select * From table1, table 2 Where num=id; num M ID N 2 b 2 p result
  • 41. Question 8 September 2022 CSE, KL University 41 Are these logically equivalent? num M 1 a 2 b 4 c table1 ID N 2 p 3 q 5 r table2 num M ID N 2 b 2 p result
  • 42. Natural Join 8 September 2022 CSE, KL University 42 ID M 1 a 2 b 4 c table1 ID N 2 p 3 q 5 r table2 ID M N 2 b p result
  • 43. Question 8 September 2022 CSE, KL University 43 Are these logically equivalent? ID M 1 a 2 b 4 c table1 ID N 2 p 3 q 5 r table2 ID M N 2 b p result
  • 44. What will be the output of following Relational Algebra Expression 8 September 2022 CSE, KL University 44
  • 45. What will be the output of following Relational Algebra Expression 8 September 2022 CSE, KL University 45
  • 46. Natural Join 8 September 2022 CSE, KL University 46 ID M 1 a 2 b 4 c table1 ID N 2 p 3 q 5 r table2 ID M N 2 b p result Natural join does not use any comparison operator. It does not concatenate the way a Cartesian product does. We can perform a Natural Join only if there is at least one common attribute that exists between two relations. In addition, the attributes must have the same name and domain. Natural join acts on those matching attributes where the values of attributes in both the relations are same.
  • 47. Problem to solve Consider three tables • SAILORS(Sal_ID, SalName, Rating, Age) • RESERVES(Sal_ID , Boat-ID, Rdate) • BOATS(Boat-ID, BoatName, Color) Write Relational Algebra express for the following i. Find all the names of Sailors who have reserved boat with ID 2 ii. Find names of sialors who have reserved RED boat iii. Find the colors of the boat reserved by Avinash 8 September 2022 CSE, KL University 47
  • 48. Problem to solve 8 September 2022 CSE, KL University 48
  • 49. Problem to solve 8 September 2022 CSE, KL University 49 Consider three tables SAILORS(Sal_ID, SalName, Rating, Age) RESERVES(Sal_ID , Boat-ID, Rdate) BOATS(Boat-ID, BoatName, Color) Write Relational Algebra express for the following i. Find all the names of Sailors who have reserved boat with ID 2 OUTPUT -------- Balaji Dinesh
  • 50. Problem to solve 8 September 2022 CSE, KL University 50 Consider three tables SAILORS(Sal_ID, SalName, Rating, Age) RESERVES(Sal_ID , Boat-ID, Rdate) BOATS(Boat-ID, BoatName, Color) Write Relational Algebra express for the following i. Find all the names of Sailors who have reserved boat with ID 2 temp1 <- σBoat_ID=2 (RESERVES) SELECT * FROM RESERVES WHERE Boat_ID = 2; temp1
  • 51. Problem to solve 8 September 2022 CSE, KL University 51 Consider three tables SAILORS(Sal_ID, SalName, Rating, Age) RESERVES(Sal_ID , Boat-ID, Rdate) BOATS(Boat-ID, BoatName, Color) Write Relational Algebra express for the following i. Find all the names of Sailors who have reserved boat with ID 2 temp1 <- σBoat_ID=2 (RESERVES) temp2 <- 𝞹SAL_ID(temp1) SELECT SAIL_ID FROM RESERVES WHERE Boat_ID = 2; temp2
  • 52. Problem to solve 8 September 2022 CSE, KL University 52 i. Find all the names of Sailors who have reserved boat with ID 2 temp1 <- σBoat_ID=2 (RESERVES) temp2 <- 𝞹SAL_ID(temp1) temp3 <- SAILORS ⋈ SAILORS.Sal_ID=temp2.Sal_ID (temp2) SELECT * FROM SAILORS NATURAL JOIN (SELECT SAL_ID FROM RESERVES WHERE Boat_ID = 2); temp3
  • 53. Problem to solve 8 September 2022 CSE, KL University 53 i. Find all the names of Sailors who have reserved boat with ID 2 temp1 <- σBoat_ID=2 (RESERVES) temp2 <- 𝞹SAL_ID(temp1) temp3 <- SAILORS ⋈ SAILORS.Sal_ID=temp2.Sal_ID (temp2) result <- 𝞹SAL_Name (temp3) SELECT SalName FROM SAILORS NATURAL JOIN (SELECT SAL_ID FROM RESERVES WHERE Boat_ID = 2); result
  • 54. Problem to solve 8 September 2022 CSE, KL University 54 i. Find all the names of Sailors who have reserved boat with ID 2 temp1 <- σBoat_ID=2 (RESERVES) temp2 <- 𝞹SAL_ID(temp1) temp3 <- SAILORS ⋈ SAILORS.Sal_ID=temp2.Sal_ID (temp2) result <- 𝞹SAL_Name (temp3)
  • 55. SAILORS(Sal_ID, SalName, Rating, Age), RESERVES(Sal_ID , Boat-ID, Rdate), BOATS(Boat-ID, BoatName, Color) 8 September 2022 CSE, KL University 55 Write Relational Algebra express for the following ii. Find names of sailors who have reserved RED boat OUTPUT -------- Balaji Dinesh
  • 56. SAILORS(Sal_ID, SalName, Rating, Age), RESERVES(Sal_ID , Boat-ID, Rdate), BOATS(Boat-ID, BoatName, Color) 8 September 2022 CSE, KL University 56 Write Relational Algebra express for the following ii. Find the colors of the boat reserved by Avinash OUTPUT -------- Red

Editor's Notes

  1. Result relation can be the input for another relational algebra operation! (Operator composition.)
  2. Result relation can be the input for another relational algebra operation! (Operator composition.)