Professional Documents
Culture Documents
R ELATIONAL A LGEBRA
Arnab Bhattacharya
arnabb@cse.iitk.ac.in
returns
ΠA1 ,...,Ak (r )
Ai , etc. are attributes of r
Select only the specified attributes A1 , . . . , Ak from all tuples of r
Duplicate rows are removed, since relations are sets
ΠA1 ,...,Ak (r )
Ai , etc. are attributes of r
Select only the specified attributes A1 , . . . , Ak from all tuples of r
Duplicate rows are removed, since relations are sets
Schema is
ΠA1 ,...,Ak (r )
Ai , etc. are attributes of r
Select only the specified attributes A1 , . . . , Ak from all tuples of r
Duplicate rows are removed, since relations are sets
Schema is changed
Applying ΠA,C on
A B C
1 1 5
1 2 5
2 3 5
2 4 8
returns
ΠA1 ,...,Ak (r )
Ai , etc. are attributes of r
Select only the specified attributes A1 , . . . , Ak from all tuples of r
Duplicate rows are removed, since relations are sets
Schema is changed
Applying ΠA,C on
A B C
1 1 5
1 2 5
2 3 5
2 4 8
A C
1 5
returns
2 5
2 8
Arnab Bhattacharya (arnabb@cse.iitk.ac.in) CS315: Relational Algebra 2019-20 4 / 37
Set Union
r ∪ s = {t|t ∈ r or t ∈ s}
Relations r and s must have the same arity (i.e., number of
attributes)
They must have same type of attribute in each column as well,
i.e., attribute domains must be compatible
If attribute names are not same, renaming should be used
Schema is
returns
returns
returns
returns
r ∩ s = {t|t ∈ r and t ∈ s}
Relations r and s must have the same arity (i.e., number of
attributes)
They must have same type of attribute in each column as well,
i.e., attribute domains must be compatible
If attribute names are not same, renaming should be used
Schema is
r ∩ s = {t|t ∈ r and t ∈ s}
Relations r and s must have the same arity (i.e., number of
attributes)
They must have same type of attribute in each column as well,
i.e., attribute domains must be compatible
If attribute names are not same, renaming should be used
Schema is not changed
Applying ∩ on
A B
A B
1 1
and 1 2
1 2
2 3
2 1
returns
r ∩ s = {t|t ∈ r and t ∈ s}
Relations r and s must have the same arity (i.e., number of
attributes)
They must have same type of attribute in each column as well,
i.e., attribute domains must be compatible
If attribute names are not same, renaming should be used
Schema is not changed
Applying ∩ on
A B
A B
1 1
and 1 2
1 2
2 3
2 1
A B
returns
1 2
r ∩ s = {t|t ∈ r and t ∈ s}
Relations r and s must have the same arity (i.e., number of
attributes)
They must have same type of attribute in each column as well,
i.e., attribute domains must be compatible
If attribute names are not same, renaming should be used
Schema is not changed
Applying ∩ on
A B
A B
1 1
and 1 2
1 2
2 3
2 1
A B
returns
1 2
r ∩ s = r − (r − s)
Arnab Bhattacharya (arnabb@cse.iitk.ac.in) CS315: Relational Algebra 2019-20 10 / 37
Join
r Zθ s = σθ (r × s)
Join is too common a query to not have its own operator
Schema is
Applying ÷ on
A B C D
1 5 2 7
1 5 3 7
1 6 3 7 C D
2 6 2 7 and 2 7 returns
2 6 3 7 3 7
3 6 2 7
3 6 3 7
3 5 3 7
Applying ÷ on
A B C D
1 5 2 7
1 5 3 7
A B
1 6 3 7 C D
1 5
2 6 2 7 and 2 7 returns
2 6
2 6 3 7 3 7
3 6
3 6 2 7
3 6 3 7
3 5 3 7
Applying ÷ on
A B C D
1 5 2 7
1 5 3 7
A B
1 6 3 7 C D
1 5
2 6 2 7 and 2 7 returns
2 6
2 6 3 7 3 7
3 6
3 6 2 7
3 6 3 7
3 5 3 7
r ÷ s = ΠR−S (r ) − ΠR−S ((ΠR−s (r ) × s) − ΠR−S,S (r ))
Find the course codes for all courses offered in the year 2018
Find the course codes for all courses offered in the year 2018
Πcoursecode (σyr=2018 (offering))
Find the course codes for all courses offered in the year 2018
Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in either of the
years 2017 and 2018
Find the course codes for all courses offered in the year 2018
Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in either of the
years 2017 and 2018
Πcoursecode (σyr=2017 (offering)) ∪ Πcoursecode (σyr=2018 (offering))
Find the course codes for all courses offered in the year 2018
Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in either of the
years 2017 and 2018
Πcoursecode (σyr=2017 (offering)) ∪ Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in the year 2017
but not in 2018
Find the course codes for all courses offered in the year 2018
Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in either of the
years 2017 and 2018
Πcoursecode (σyr=2017 (offering)) ∪ Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in the year 2017
but not in 2018
Πcoursecode (σyr=2017 (offering)) − Πcoursecode (σyr=2018 (offering))
Find the course codes for all courses offered in the year 2018
Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in either of the
years 2017 and 2018
Πcoursecode (σyr=2017 (offering)) ∪ Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in the year 2017
but not in 2018
Πcoursecode (σyr=2017 (offering)) − Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in both the years
2017 and 2018
Find the course codes for all courses offered in the year 2018
Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in either of the
years 2017 and 2018
Πcoursecode (σyr=2017 (offering)) ∪ Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in the year 2017
but not in 2018
Πcoursecode (σyr=2017 (offering)) − Πcoursecode (σyr=2018 (offering))
Find the course codes for all the courses offered in both the years
2017 and 2018
Πcoursecode (σyr=2017 (offering)) ∩ Πcoursecode (σyr=2018 (offering))
Arnab Bhattacharya (arnabb@cse.iitk.ac.in) CS315: Relational Algebra 2019-20 18 / 37
Example Queries
Find the titles of all courses offered in the year 2018
Find the years when all the courses of type 5 were offered
Find the years when all the courses of type 5 were offered
(Πcoursecode,yr (offering)) ÷ ct
Arnab Bhattacharya (arnabb@cse.iitk.ac.in) CS315: Relational Algebra 2019-20 19 / 37
Extended Relational Algebra
returns
Aggregate functions that can be used are avg, min, max, sum,
count
Can be applied on groups of tuples as well
Aggregate operation is of the form G1 ,...,Gk GF1 (A1 ),...,Fn (An ) (E) where
G1 , . . . , Gk is the list of attributes on which to group (may be empty)
Each Fi is an aggregate function that operates on the attribute Ai
Applying Gsum(C) on r
A B C
1 1 5
1 2 5 returns
2 3 5
2 4 8
Aggregate functions that can be used are avg, min, max, sum,
count
Can be applied on groups of tuples as well
Aggregate operation is of the form G1 ,...,Gk GF1 (A1 ),...,Fn (An ) (E) where
G1 , . . . , Gk is the list of attributes on which to group (may be empty)
Each Fi is an aggregate function that operates on the attribute Ai
Applying Gsum(C) on r
A B C
1 1 5
sum(C)
1 2 5 returns
23
2 3 5
2 4 8
A B A C
1 5 1 7
Z =
2 6 2 8
3 7 4 9
A B A C
A B C
1 5 1 7
Z = 1 5 7
2 6 2 8
2 6 8
3 7 4 9
A B A C
1 5 1 7
AY =
2 6 2 8
3 7 4 9
A B A C
A B C
1 5 1 7
Z = 1 5 7
2 6 2 8
2 6 8
3 7 4 9
A B A C A B C
1 5 1 7 1 5 7
AY =
2 6 2 8 2 6 8
3 7 4 9 3 7 null
A B A C
1 5 1 7
X@ =
2 6 2 8
3 7 4 9
A B A C
A B C
1 5 1 7
Z = 1 5 7
2 6 2 8
2 6 8
3 7 4 9
A B A C A B C
1 5 1 7 1 5 7
AY =
2 6 2 8 2 6 8
3 7 4 9 3 7 null
A B A C A B C
1 5 1 7 1 5 7
X@ =
2 6 2 8 2 6 8
3 7 4 9 4 null 9
A B A C
1 5 1 7
Z =
2 6 2 8
3 7 4 9
A B A C
A B C
1 5 1 7
Z = 1 5 7
2 6 2 8
2 6 8
3 7 4 9
A B A C
1 5 1 7
A[@ =
2 6 2 8
3 7 4 9
A B A C
A B C
1 5 1 7
Z = 1 5 7
2 6 2 8
2 6 8
3 7 4 9
A B C
A B A C
1 5 7
1 5 1 7
A[@ = 2 6 8
2 6 2 8
3 7 null
3 7 4 9
4 null 9
For each instructor, find the total number of courses offered by her
in the year 2018
For each instructor, find the total number of courses offered by her
in the year 2018
instructor Gcount(coursecode) (σyr=2018 (offering))
For each instructor, find the total number of courses offered by her
in the year 2018
instructor Gcount(coursecode) (σyr=2018 (offering))
For each instructor, find the total number of courses offered by her
per year
For each instructor, find the total number of courses offered by her
in the year 2018
instructor Gcount(coursecode) (σyr=2018 (offering))
For each instructor, find the total number of courses offered by her
per year
instructor,yr Gcount(coursecode) (offering)
For each instructor, find the total number of courses offered by her
in the year 2018
instructor Gcount(coursecode) (σyr=2018 (offering))
For each instructor, find the total number of courses offered by her
per year
instructor,yr Gcount(coursecode) (offering)
For each course, indicate the most recent year it was offered
For each instructor, find the total number of courses offered by her
in the year 2018
instructor Gcount(coursecode) (σyr=2018 (offering))
For each instructor, find the total number of courses offered by her
per year
instructor,yr Gcount(coursecode) (offering)
For each course, indicate the most recent year it was offered
course-offering ← coursecode Gmax(yr) (offering)
course-year ← ρ(code,yr) (Πcoursecode,max(yr) (course-offering))
course AY course-year
Arnab Bhattacharya (arnabb@cse.iitk.ac.in) CS315: Relational Algebra 2019-20 27 / 37
Null Values
A B C
1 1 5
returns
2 3 5
2 4 8