Jennifer Widom
Relational Databases
Relational Algebra (1)
Select, project, join
Jennifer Widom
Relational Algebra (1)
Query (expression) on set of relations produces
relation as a result
Jennifer Widom
Relational Algebra (1)
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
College Student Apply
Jennifer Widom
Simplest query: relation name
cName state enr sID sName GPA HS sID cName major dec
College Student Apply
Relational Algebra (1)
Use operators to filter, slice, combine
Jennifer Widom
Select operator: picks certain rows
Students with GPA>3.7
Students with GPA>3.7 and HS<1000
Applications to Stanford CS major
cName state enr sID sName GPA HS sID cName major dec
College Student Apply
Relational Algebra (1)
Jennifer Widom
Project operator: picks certain columns
ID and decision of all applications
cName state enr sID sName GPA HS sID cName major dec
College Student Apply
Relational Algebra (1)
Jennifer Widom
To pick both rows and columns…
ID and name of students with GPA>3.7
cName state enr sID sName GPA HS sID cName major dec
College Student Apply
Relational Algebra (1)
Jennifer Widom
Duplicates
List of application majors and decisions
cName state enr sID sName GPA HS sID cName major dec
College Student Apply
Relational Algebra (1)
Jennifer Widom
Cross-product: combine two relations
(a.k.a. Cartesian product)
cName state enr sID sName GPA HS sID cName major dec
College Student Apply
Relational Algebra (1)
Jennifer Widom
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
College Student Apply
Relational Algebra (1)
Jennifer Widom
Natural Join
 Enforce equality on all attributes with same name
 Eliminate one copy of duplicate attributes
cName state enr sID sName GPA HS sID cName major dec
College Student Apply
Relational Algebra (1)
Jennifer Widom
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
College Student Apply
Relational Algebra (1)
Names and GPAs of students with HS>1000 who applied to CS
at college with enr>20,000 and were rejected
Jennifer Widom
Natural Join
cName state enr sID sName GPA HS sID cName major dec
College Student Apply
Relational Algebra (1)
Jennifer Widom
Theta Join
 Basic operation implemented in DBMS
 Term “join” often means theta join
cName state enr sID sName GPA HS sID cName major dec
College Student Apply
Relational Algebra (1)
Jennifer Widom
Relational Algebra (1)
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

Relational algebra1

  • 1.
    Jennifer Widom Relational Databases RelationalAlgebra (1) Select, project, join
  • 2.
    Jennifer Widom Relational Algebra(1) Query (expression) on set of relations produces relation as a result
  • 3.
    Jennifer Widom Relational Algebra(1) 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 College Student Apply
  • 4.
    Jennifer Widom Simplest query:relation name cName state enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1) Use operators to filter, slice, combine
  • 5.
    Jennifer Widom Select operator:picks certain rows Students with GPA>3.7 Students with GPA>3.7 and HS<1000 Applications to Stanford CS major cName state enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1)
  • 6.
    Jennifer Widom Project operator:picks certain columns ID and decision of all applications cName state enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1)
  • 7.
    Jennifer Widom To pickboth rows and columns… ID and name of students with GPA>3.7 cName state enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1)
  • 8.
    Jennifer Widom Duplicates List ofapplication majors and decisions cName state enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1)
  • 9.
    Jennifer Widom Cross-product: combinetwo relations (a.k.a. Cartesian product) cName state enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1)
  • 10.
    Jennifer Widom Cross-product: combinetwo 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 College Student Apply Relational Algebra (1)
  • 11.
    Jennifer Widom Natural Join Enforce equality on all attributes with same name  Eliminate one copy of duplicate attributes cName state enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1)
  • 12.
    Jennifer Widom Natural Join Namesand GPAs of students with HS>1000 who applied to CS and were rejected cName state enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1) Names and GPAs of students with HS>1000 who applied to CS at college with enr>20,000 and were rejected
  • 13.
    Jennifer Widom Natural Join cNamestate enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1)
  • 14.
    Jennifer Widom Theta Join Basic operation implemented in DBMS  Term “join” often means theta join cName state enr sID sName GPA HS sID cName major dec College Student Apply Relational Algebra (1)
  • 15.
    Jennifer Widom Relational Algebra(1) 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