Relational Algebra
Relational Algebra
• The relational algebra consists of a set of operators
that (speaking very loosely) allow us to derive
“new” relations from “old” ones.
• Each such operator takes one or more relations as
input and produces another relation as output.
Five operators:
– Union: 
– Intersection: 
– Difference: -
– Selection: s
– Projection: P
– Cartesian Product: 
– Rename : r
1. Union
2. Intersection
• It is used to derive common record
• R1  R2
3. Difference
3. Selection
• Returns all tuples which satisfy a condition
• Notation: sc(R)
• Examples
– sSalary > 40000 (Employee)
– sname = “Smith” (Employee)
• The condition c can be =, <, , >, , <>
sSalary > 400000 (Employee)
SSN Name Salary
1234545 John 200000
5423341 Smith 600000
4352342 Fred 500000
SSN Name Salary
5423341 Smith 600000
4352342 Fred 500000
Another selection
4. Projection
• Eliminates columns, then removes duplicates
• Notation: P A1,…,An (R)
• Example: project social-security number and
names:
– P SSN, Name (Employee)
– Output schema: Answer(SSN, Name)
P Name,Salary (Employee)
SSN Name Salary
1234545 John 200000
5423341 John 600000
4352342 John 200000
Name Salary
John 20000
John 60000
Example of Projection
Projection removes duplicates
5. Cartesian Product
• Each tuple in R1 with each tuple in R2
• Notation: R1  R2
• Example:
– Employee  Dependents
• Very rare in practice; mainly used to express
joins
Cartesian Product Example
Employee
Name SSN
John 999999999
Tony 777777777
Dependents
EmployeeSSN Dname
999999999 Emily
777777777 Joe
Employee x Dependents
Name SSN EmployeeSSN Dname
John 999999999 999999999 Emily
John 999999999 777777777 Joe
Tony 777777777 999999999 Emily
Tony 777777777 777777777 Joe
Renaming
• Changes the schema, not the instance
• Notation: r B1,…,Bn (R)
• Example:
– rLastName, SocSocNo (Employee)
– Output schema:
Answer(LastName, SocSocNo)
Renaming Example
Employee
Name SSN
John 999999999
Tony 777777777
LastName SocSocNo
John 999999999
Tony 777777777
rLastName, SocSocNo (Employee)

2. Relational Algebra.ppt

  • 1.
  • 2.
    Relational Algebra • Therelational algebra consists of a set of operators that (speaking very loosely) allow us to derive “new” relations from “old” ones. • Each such operator takes one or more relations as input and produces another relation as output.
  • 3.
    Five operators: – Union: – Intersection:  – Difference: - – Selection: s – Projection: P – Cartesian Product:  – Rename : r
  • 5.
  • 7.
    2. Intersection • Itis used to derive common record • R1  R2
  • 8.
  • 10.
    3. Selection • Returnsall tuples which satisfy a condition • Notation: sc(R) • Examples – sSalary > 40000 (Employee) – sname = “Smith” (Employee) • The condition c can be =, <, , >, , <>
  • 11.
    sSalary > 400000(Employee) SSN Name Salary 1234545 John 200000 5423341 Smith 600000 4352342 Fred 500000 SSN Name Salary 5423341 Smith 600000 4352342 Fred 500000
  • 13.
  • 14.
    4. Projection • Eliminatescolumns, then removes duplicates • Notation: P A1,…,An (R) • Example: project social-security number and names: – P SSN, Name (Employee) – Output schema: Answer(SSN, Name)
  • 15.
    P Name,Salary (Employee) SSNName Salary 1234545 John 200000 5423341 John 600000 4352342 John 200000 Name Salary John 20000 John 60000
  • 16.
  • 17.
  • 18.
    5. Cartesian Product •Each tuple in R1 with each tuple in R2 • Notation: R1  R2 • Example: – Employee  Dependents • Very rare in practice; mainly used to express joins
  • 19.
    Cartesian Product Example Employee NameSSN John 999999999 Tony 777777777 Dependents EmployeeSSN Dname 999999999 Emily 777777777 Joe Employee x Dependents Name SSN EmployeeSSN Dname John 999999999 999999999 Emily John 999999999 777777777 Joe Tony 777777777 999999999 Emily Tony 777777777 777777777 Joe
  • 20.
    Renaming • Changes theschema, not the instance • Notation: r B1,…,Bn (R) • Example: – rLastName, SocSocNo (Employee) – Output schema: Answer(LastName, SocSocNo)
  • 21.
    Renaming Example Employee Name SSN John999999999 Tony 777777777 LastName SocSocNo John 999999999 Tony 777777777 rLastName, SocSocNo (Employee)