DBMS Relational Algebra
DBMS Relational Algebra
Relational Algebra
• Query (expression) on set of relation will produce
relation as a result.
• Examples: simple college admissions
database
• College(cName,state,enrollment)
• Student(sID,sName,GPA,sizeHS)
• Apply(sID,cName,major,decision)
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 9.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 500 1 William CS N
UoI IL 400 4 Jack 5.0 100 3 MIT CS Y
Simplest query: relation name
Use operators to filter, slice, combine
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 9.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 500 1 William CS N
UoI IL 400 4 Jack 5.0 100 2 William CS Y
Select operator: picks certain rows
Students with GPA>3.7
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Project operator: picks certain columns
ID and decision of all applications
sID dec
3 Y
4 N
1 N
2 Y
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
sID sName GPA HS
To pick both rows and columns… sID sName
1 Victor 8.6 700
1 Victor
ID and name of students with GPA>3.7 2 Mike 2.2 200
3 William
3 William 7.3 1500
4 Jack
4 Jack 5.0 700
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Duplicates Major dec
List of application majors and decisions CS Y
EE N
CS N
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Cross-product: combine two relations
(a.k.a. Cartesian product)
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 3 Stanford CS Y
1 Victor 8.6 700 4 MIT EE N
1 Victor 8.6 700 1 William CS N
1 Victor 8.6 700 2 William CS Y
2 Mike 2.2 200 3 Stanford CS Y
2 Mike 2.2 200 4 MIT EE N
2 Mike 2.2 200 1 William CS N
2 Mike 2.2 200 2 William CS Y
3 William 7.3 1500 3 Stanford CS Y
3 William 7.3 1500 4 MIT EE N
3 William 7.3 1500 1 William CS N
3 William 7.3 1500 2 William CS Y
4 Jack 5.0 700 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
4 Jack 5.0 700 1 William CS N
4 Jack 5.0 700 2 William CS Y
Cross-product: combine two relations
(a.k.a. Cartesian product)
Names and GPAs of students with HS>1000 who applied to CS
and were rejected
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
Cartesian product
Names and GPAs of students with HS>1000 who applied to CS
and were rejected
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Natural Join
Names and GPAs of students with HS>1000 who applied to CS
and were rejected
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Natural Join
Names and GPAs of students with HS>1000 who applied to CS
at
andcollege with enr>20,000 and were rejected
were rejected
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Natural Join
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Theta Join
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Theta Join
Names and GPAs of students with HS>1000 who applied to CS
and were rejected
𝜋𝑠𝑁𝑎𝑚𝑒,𝐺𝑃𝐴
(Student ⋈𝐻𝑆>1000 ∧ 𝑀𝑎𝑗𝑜𝑟=′ 𝐶𝑆 ′ ∧ 𝑑𝑒𝑐=′ 𝑁′ ^ 𝑆𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦)
Student.sID
Student.sID sName
sName GPA
GPA HS
HS Apply.sID
Apply.sID cName
cName Major
Major Dec
Dec
11
2 Victor
Victor
Mike 8.6
8.6
2.2 700
700
1200 11
2 William
William CS
CS N
YN
22 Mike
Mike 2.2
2.2 1200
200 22 William
William CS
CS YY
33 William
William 7.3
7.3 1500
1500 33 Stanford
Stanford CS
CS YY
44 Jack
Jack 5.0
5.0 700
700 44 MIT
MIT EE
EE N
N
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 1200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
EquiJoin
Join where only comparison operator used is =
• The result of equijoin always have one or more pair of
attributes ( whose names need not be identical ) that have
identical tuples
• Special case: condition c contains only conjunction of
equalities
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
EquiJoin
Names and GPAs of students who applied to CS and were rejected
𝜋𝑠𝑁𝑎𝑚𝑒,𝐺𝑃𝐴
(Student ⋈𝑀𝑎𝑗𝑜𝑟=′ 𝐶𝑆 ′ ∧ 𝑆𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 ∧ 𝑑𝑒𝑐=′ 𝑁′ 𝐴𝑝𝑝𝑙𝑦)
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 1 William CS N
2 Mike 2.2 1200 2 William CS Y
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
College Student Apply
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Join
Natural Join (𝑹 ⋈ 𝑺)
Apply on same name attribute
Same name attribute get merged
Condition called implicitly of equality of same name
attribute ( need not to write in query)
Theta Join (𝑹 ⋈𝜽 𝑺)
Here 𝜽 is condition on which join will take place
Condition will be made up of {<,>,≥,≤,≠,=} and Boolean
operators {AND, OR, NOT}
No same name attribute merging takes place
Equijoin (𝐑 ⋈<𝒄𝒐𝒏𝒅 𝒐𝒇 𝒆𝒒𝒖𝒂𝒍𝒊𝒕𝒚> 𝑺)
Special case of theta join
Condition is made up of {=,AND,OR,NOT} i.e. = will be
only comparative operator
No attribute merging of same name takes place
Query (expression) on set of relations produces
relation as a result
Simplest query: relation name
Use operators to filter, slice, combine
Operators so far: select, project, cross-product,
natural join, theta join
Union operator cName
William
List of college and student names No
Duplicates Stanford
MIT
UoI
Victor
Mike
Jack
𝜋𝑐𝑁𝑎𝑚𝑒 𝐶𝑜𝑙𝑙𝑒𝑔𝑒 ∪ 𝜋𝑠𝑁𝑎𝑚𝑒 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
College Student Apply
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Difference operator
IDs of students who didn’t apply anywhere
sID
2
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
Difference operator
IDs and names of
of students students
who who didn’t
didn’t apply apply anywhere
anywhere
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
Intersection operator
Names that are both a college name and a student name
cName
William
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Intersection doesn’t add expressive power (1)
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Intersection doesn’t add expressive power (2)
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Rename operator
1.
2.
3.
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Rename operator C
To unify schemas for set operators
List of college and student names William
Stanford
MIT
UoI
Victor
Mike
Jack
College Student Apply
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Rename operator
For disambiguation in “self-joins”
Pairs of colleges in same state
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Division operator
For query that need information of ‘all’
List of names of college where all student who apply in CS
major have applied
Apply
3 Stanford 1 1
4 MIT 2 2
1 William 3 3
Apply
2 William
2 Stanford sID cName Major dec
College 1 Student
Stanford
3 Stanford CS Y
cName state enr sID sName GPA HS sID
4 cName
MIT Major
EE dec
N
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database tuple
Other Aggregate Functions
• Sum – find total of a particular attribute like total HSsize
• Count – Count will count no. of tuples like no. of Student
• Maximum - find the maximum among the particular set like
maximum GPA
• Minimum – find the minimum like minimum GPA
• Average – find average among a set like average GPA
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database tuple
Resulting table will have column name
<function>_<attribute>
Eg. COUNT_sID, AVERAGE_GPA
Duplicates are not eliminated when aggregate function is
applied
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database
Find Total High School size
𝔉 𝑇𝑂𝑇𝐴𝐿 𝐻𝑆 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 3100
Find Maximum, Minimum & Average GPA of students
𝔉𝑀𝐴𝑋𝐼𝑀𝑈𝑀𝐺𝑃𝐴 ,𝑀𝐼𝑁𝐼𝑀𝑈𝑀𝐺𝑃𝐴 ,𝐴𝑉𝐸𝑅𝐴𝐺𝐸𝐺𝑃𝐴 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
MAXIMUM_GPA MINIMUM_GPA AVERAGE_GPA
8.6 2.2 5.8
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database
Grouping will group tuples by value of some
attributes
And then applying aggregate function independently
to each group
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database
Find number of colleges in each state and their average
enrollment
𝑎𝑠𝑡𝑎𝑡𝑒 𝔉𝐶𝑂𝑈𝑁𝑇𝑐𝑁𝑎𝑚𝑒,𝐴𝑉𝐸𝑅𝐴𝐺𝐸𝑒𝑛𝑟 College
state COUNT_cName AVERAGE_enr
CA 2 450
MA 1 300
IL 1 400
College Student Apply
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database
Find number of apply in colleges at California
𝑎𝑠𝑡𝑎𝑡𝑒 𝔉𝐶𝑂𝑈𝑁𝑇𝑠𝐼𝐷 𝐶𝑜𝑙𝑙𝑒𝑔𝑒 ⋈ 𝐴𝑝𝑝𝑙𝑦
𝜎𝑠𝑡𝑎𝑡𝑒=′ 𝐶𝐴′ (𝑠𝑡𝑎𝑡𝑒 𝔉𝐶𝑂𝑈𝑁𝑇𝑠𝐼𝐷 (𝐶𝑜𝑙𝑙𝑒𝑔𝑒 ⋈ 𝐴𝑝𝑝𝑙𝑦))
state COUNT_sID
state COUNT_sID
CA 3
CA 3
MA 1
IL 0
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Outer Join
For including tuples that not satisfying the join condition
List all students and mention College if they apply in a college
𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ℎ𝑠𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 1 William CS N
2 Mike 2.2 200 NULL NULL NULL NULL
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
Left Outer Join: For including tuples of LEFT RELATION that
not satisfying the join condition
List all students and mention College if they apply in a college
𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ℎ𝑠𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 1 William CS N
2 Mike 2.2 200 NULL NULL NULL NULL
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
4 Jack 5.0 700 4 Stanford CS Y
College Student Apply
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 4 Stanford CS Y
UoI IL 400 4 Jack 5.0 700 1 William CS N
Right Outer Join: For including tuples of Right RELATION that
not satisfying the join condition
List all College and student details who have applied there
𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ℎ𝑠𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦
Student.sID sName GPA HS Apply.sID cName Major Dec
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
1 Victor 8.6 700 1 William CS N
NULL NULL NULL NULL 5 UoI MBA Y
3 William 7.3 1500 3 MIT CS Y
Apply
College Student
sID cName Major dec
cName state enr sID sName GPA HS
3 Stanford CS Y
William CA 500 1 Victor 8.6 700 4 MIT EE N
Stanford CA 400 2 Mike 2.2 200 1 William CS N
MIT MA 300 3 William 7.3 1500 5 UoI MBA Y
UoI IL 400 4 Jack 5.0 700 3 MIT CS Y
Full Outer Join: For including tuples of RELATIONs on both
side of join that not satisfying the join condition
List all students and mention College if they apply in a college
𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ℎ𝑠𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 1 William CS N
2 Mike 2.2 200 NULL NULL NULL NULL
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
NULL NULL NULL NULL 5 UoI MBA Y
College Student Apply
cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
5 UoI MBA Y
Outer Join: For including tuples of RELATIONs that not
satisfying the join condition
PEOPLE: MENU:
Name Age Food Food Day
Alice 21 Hamburger Pizza Monday
Bill 24 Pizza Hamburger Tuesday
Chicken Wednesday
Carl 23 Beer
Pasta Thursday
Dina 19 Shrimp
Tacos Friday
Right Outer Join
PEOPLE people.food = menu.food MENU