SlideShare a Scribd company logo
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

What's hot (20)

Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
Transaction management
Transaction managementTransaction management
Transaction management
 
JDBC
JDBCJDBC
JDBC
 
Computer Science:Java jdbc
Computer Science:Java jdbcComputer Science:Java jdbc
Computer Science:Java jdbc
 
Sql commands
Sql commandsSql commands
Sql commands
 
MySQL Workbench Tutorial | Introduction To MySQL Workbench | MySQL DBA Traini...
MySQL Workbench Tutorial | Introduction To MySQL Workbench | MySQL DBA Traini...MySQL Workbench Tutorial | Introduction To MySQL Workbench | MySQL DBA Traini...
MySQL Workbench Tutorial | Introduction To MySQL Workbench | MySQL DBA Traini...
 
MySQL Basics
MySQL BasicsMySQL Basics
MySQL Basics
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
MySQL
MySQLMySQL
MySQL
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 
Clean code
Clean codeClean code
Clean code
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
 
ACID Property in DBMS
ACID Property in DBMSACID Property in DBMS
ACID Property in DBMS
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
 
C# Lab Programs.pdf
C# Lab Programs.pdfC# Lab Programs.pdf
C# Lab Programs.pdf
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
C programming for problem solving
C programming for problem solving  C programming for problem solving
C programming for problem solving
 
Sql functions
Sql functionsSql functions
Sql functions
 
Shell programming
Shell programmingShell programming
Shell programming
 
SQLite - Overview
SQLite - OverviewSQLite - Overview
SQLite - Overview
 

Similar to Relational Algebra1.pptx

relational algebra and it's implementation
relational algebra and it's implementationrelational algebra and it's implementation
relational algebra and it's implementationdbmscse61
 
Introduction to Relational Database Management Systems
Introduction to Relational Database Management SystemsIntroduction to Relational Database Management Systems
Introduction to Relational Database Management SystemsAdri Jovin
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusemailharmeet
 
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 pptxdanishriasat792
 
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 systemsmeharikiros2
 
Java Database Connectivity with JDBC.pptx
Java Database Connectivity with JDBC.pptxJava Database Connectivity with JDBC.pptx
Java Database Connectivity with JDBC.pptxtakomatiesucy
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational AlgebraPyingkodi Maran
 
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.pptMisganawAbeje1
 
Lecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxLecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxHanzlaNaveed1
 
MODULE-2dbmsmodule2chapter1isedepts.pptx
MODULE-2dbmsmodule2chapter1isedepts.pptxMODULE-2dbmsmodule2chapter1isedepts.pptx
MODULE-2dbmsmodule2chapter1isedepts.pptxtejashreebsise
 
Relational Model
Relational ModelRelational Model
Relational ModelSiti Ismail
 

Similar to Relational Algebra1.pptx (20)

Unit3_DIS.ppt
Unit3_DIS.pptUnit3_DIS.ppt
Unit3_DIS.ppt
 
relational algebra and it's implementation
relational algebra and it's implementationrelational algebra and it's implementation
relational algebra and it's implementation
 
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
 
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

The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringDenish Jangid
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFVivekanand Anglo Vedic Academy
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfYibeltalNibretu
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chipsGeoBlogs
 

Recently uploaded (20)

The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 

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.)