Relational Algebra and Calculus
Relational Algebra and Calculus
Calculus
Francis Noel I. Alarcon
University of the East
Query Language: Definition
• σ Customer_City="Agra" (CUSTOMER)
Customer_Id Customer_Name Customer_City
C10100 Steve Agra
C10111 Raghu Agra
Relational Algebra: Project (∏)
• ∏C115
Student_Name
Lucy (COURSE)
S931 ∪ ∏ Student_Name
S951 (STUDENT)
Rick 18
Student_Name
Aditya
Steve
Paul
Lucy
Carl
Rick
Relational Algebra: Set Difference (-)
• ∏C115
Student_Name
Lucy (STUDENT)
S931 - S951
∏ Student_Name (COURSE)
Rick 18
Student_Name
Carl
Rick
Relational Algebra: Cartesian product (X)
• RXS
Col_A Col_B Col_X Col_Y
AA 100 XX 50
AA 100 XY 25
AB 200 XX 50
AB 200 XY 25
Relational Algebra: Rename (ρ)
• ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
CUST_NAMES
Steve
Raghu
Chaitanya
Ajeet
Carl
Relational Algebra: Natural Join (⋈)
• STUDENT ⋈ COURSE
Course_Id Student_Id Student_Name Student_Age
C104 S901 Aditya 19
C106 S911 Steve 18
C109 S921 Paul 19
Relational Algebra: Left outer join (⟕)
• STUDENT ⟕COURSE
Course_Id Student_Id Student_Name Student_Age
C104 S901 Aditya 19
C106 S911 Steve 18
C109 S921 Paul 19
C110 S933 NULL NULL
Relational Algebra: Right outer join (⟖)
• Right outer join contains the set of tuples of all combinations in R and S that
are equal on their common attribute names.
• In right outer join, tuples in S have no matching tuples in R.
• It is denoted by ⟖.
Relational Algebra: Example
• Table Course
Course_Id
Table Student
Student_Id
Student_Id Student_Name Student_Age
C104 S901 S901 Aditya 19
C106 S911 S911 Steve 18
C109 S921 S921 Paul 19
C110 S933 S944 Mike 20
• STUDENT ⟖COURSE
Student_Id Course_Id Student_Name Student_Age
S901 C104 Aditya 19
S911 C106 Steve 18
S921 C109 Paul 19
S944 NULL Mike 20
Relational Algebra: Full outer join (⟗)
• Full outer join is like a left or right join except that it contains all rows from
both tables.
• In full outer join, tuples in R that have no matching tuples in S and tuples in S
that have no matching tuples in R in their common attribute name.
• It is denoted by ⟗.
Relational Algebra: Example
• Table Course
Course_Id
Table Student
Student_Id
Student_Id Student_Name Student_Age
C104 S901 S901 Aditya 19
C106 S911 S911 Steve 18
C109 S921 S921 Paul 19
C110 S933 S944 Mike 20
• STUDENT ⟖COURSE
Student_Id Course_Id Student_Name Student_Age
S901 C104 Aditya 19
S911 C106 Steve 18
S921 C109 Paul 19
S933 C110 NULL NULL
S944 NULL Mike 20
Relational Algebra: Intersection (∩)
• ∏C115
Student_Name
Lucy (COURSE)
S931 ∩ ∏ Student_Name
S951 (STUDENT)
Rick 18
Student_Name
Aditya
Steve
Paul
Relational Algebra: Division (÷)
• In domain relational calculus the records are filtered based on the domains.
• Notation
• { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}