The Relational Algebra
Lecture 3
Assistant Lecturer Huda A. Alameen
hudaa.alameen@uokufa.edu.iq
The Relational Algebra:
 Relational algebra is the basic set of operations for the
relational model
 These operations enable a user to specify basic retrieval
requests (or queries)
 The result of an operation is a new relation, which may
have been formed from one or more input relations
The fundamental operations in the
relational algebra:
 Unary operations: are called unary operations, because they operate on
one relation.
▫Select.
▫Project.
 Binary operations: operate on pairs of relations and are, therefore, called
binary operations.
▫Union.
▫Intersection
▫set difference.
▫Cartesian product.
1- The Select Operation
 The select operation selects tuples that satisfy a given
predicate.
 We use the lowercase Greek letter sigma (Ϭ) to denote
selection
 In general, the SELECT operation is denoted by
σ<selection condition>(R)
To select tuples of the instructor relation where the instructor is in the “Physics”
department, we write:
Ϭ dept_name =“Physics” (instructor )
•The Result will be
The Select Operation
2- The Project Operation
The Project Operation
3- The Union Operation
 For a union operation r ∪ s to be valid, these two conditions must be
hold:
 The relations r and s must have the same number of attributes.
 The domains of the ith attribute of r and the ith attribute of s must be
the same, for all i.
4- The Intersection:
 Intersection is denoted by ∩
 The result of this operation, denoted by R ∩ S, is a
relation that includes all tuples that are in both R and S.
 The attribute names in the result will be the same as the
attribute names in R
 The two operand relations R and S must be “type
compatible”
5- The Set-Difference Operation
 The set-difference operation, denoted by (−)
 allows us to find tuples that are in one relation but are
not in another.
 The expression r − s produces a relation containing those
tuples in r but not in s.
6- The Cartesian-Product Operation
 denoted by a cross (×).
 allows us to combine information from any two relations.
 We write the Cartesian product of relations r1 and r2 as r1
× r2.
The Binary Relational Operation: Join
The Binary Relational Operation: Join
Binary Relational Operations: NATURAL
JOIN Operation
Binary Relational Operations: NATURAL
JOIN Operation
Example of NATURAL JOIN operation
Binary Relational Operations: Outer JOIN
Operation
 The outer-join operation is an extension of the join
operation to deal with missing information.
 Some tuples in either or both of the relations being joined
may be lost.
 The outer join operation works in a manner similar to the
natural join operation but preserves those tuples that
would be lost in an join.
 This is done by creating tuples in the result containing null
values.
Binary Relational Operations: Outer JOIN
Operation
 There are three forms of the operation:
1. left outer join, denoted ⟕: takes all tuples in the left relation that did not match with any
tuple in the right relation, pads the tuples with null values for all other attributes from the right
relation, and adds them to the result of the natural join.
2. right outer join, denoted ⟖: is symmetric with the left outer join: It pads tuples from the right
relation that did not match any from the left relation with nulls and adds them to the result of
the natural join.
3. Full outer join ⟗: does both the left and right outer join operations, padding tuples from the
left relation that did not match any from the right relation, as well as tuples from the right
relation that did not match any from the left relation, and adding them to the result of the join.
Relational algebra
Relational algebra
Relational algebra
Relational algebra

Relational algebra

  • 1.
    The Relational Algebra Lecture3 Assistant Lecturer Huda A. Alameen hudaa.alameen@uokufa.edu.iq
  • 2.
    The Relational Algebra: Relational algebra is the basic set of operations for the relational model  These operations enable a user to specify basic retrieval requests (or queries)  The result of an operation is a new relation, which may have been formed from one or more input relations
  • 3.
    The fundamental operationsin the relational algebra:  Unary operations: are called unary operations, because they operate on one relation. ▫Select. ▫Project.  Binary operations: operate on pairs of relations and are, therefore, called binary operations. ▫Union. ▫Intersection ▫set difference. ▫Cartesian product.
  • 4.
    1- The SelectOperation  The select operation selects tuples that satisfy a given predicate.  We use the lowercase Greek letter sigma (Ϭ) to denote selection  In general, the SELECT operation is denoted by σ<selection condition>(R)
  • 5.
    To select tuplesof the instructor relation where the instructor is in the “Physics” department, we write: Ϭ dept_name =“Physics” (instructor ) •The Result will be
  • 6.
  • 7.
    2- The ProjectOperation
  • 8.
  • 9.
    3- The UnionOperation  For a union operation r ∪ s to be valid, these two conditions must be hold:  The relations r and s must have the same number of attributes.  The domains of the ith attribute of r and the ith attribute of s must be the same, for all i.
  • 14.
    4- The Intersection: Intersection is denoted by ∩  The result of this operation, denoted by R ∩ S, is a relation that includes all tuples that are in both R and S.  The attribute names in the result will be the same as the attribute names in R  The two operand relations R and S must be “type compatible”
  • 15.
    5- The Set-DifferenceOperation  The set-difference operation, denoted by (−)  allows us to find tuples that are in one relation but are not in another.  The expression r − s produces a relation containing those tuples in r but not in s.
  • 17.
    6- The Cartesian-ProductOperation  denoted by a cross (×).  allows us to combine information from any two relations.  We write the Cartesian product of relations r1 and r2 as r1 × r2.
  • 19.
    The Binary RelationalOperation: Join
  • 20.
    The Binary RelationalOperation: Join
  • 22.
    Binary Relational Operations:NATURAL JOIN Operation
  • 23.
    Binary Relational Operations:NATURAL JOIN Operation
  • 24.
    Example of NATURALJOIN operation
  • 25.
    Binary Relational Operations:Outer JOIN Operation  The outer-join operation is an extension of the join operation to deal with missing information.  Some tuples in either or both of the relations being joined may be lost.  The outer join operation works in a manner similar to the natural join operation but preserves those tuples that would be lost in an join.  This is done by creating tuples in the result containing null values.
  • 26.
    Binary Relational Operations:Outer JOIN Operation  There are three forms of the operation: 1. left outer join, denoted ⟕: takes all tuples in the left relation that did not match with any tuple in the right relation, pads the tuples with null values for all other attributes from the right relation, and adds them to the result of the natural join. 2. right outer join, denoted ⟖: is symmetric with the left outer join: It pads tuples from the right relation that did not match any from the left relation with nulls and adds them to the result of the natural join. 3. Full outer join ⟗: does both the left and right outer join operations, padding tuples from the left relation that did not match any from the right relation, as well as tuples from the right relation that did not match any from the left relation, and adding them to the result of the join.