Relational Algebra
Relational Algebra
1
CONTENT
Examples:
Select the EMPLOYEE tuples whose department number is
4:
(EMPLOYEE)
Select the employee tuples whose salary is greater than
$30,000:
(EMPLOYEE)
UNARY RELATIONAL OPERATIONS: SELECT
In general, the select operation is denoted by
<selection condition>(R) where
the symbol (sigma) is used to denote the select operator
the selection condition is a Boolean (conditional)
expression specified on the attributes of relation R
tuples that make the condition true are selected
appear in the result of the operation
tuples that make the condition false are filtered out
discarded from the result of the operation
Name Age Department
Alice 30 HR
Bob 25 IT
Carol 40 IT
UNARY RELATIONAL OPERATIONS: SELECT
SELECT Operation Properties
The SELECT operation
<selection condition>(R) produces a
relation
S that has the same schema (same attributes) as R
SELECT is commutative:
<condition1>( < condition2> (R)) =
<condition2> ( < condition1> (R))
Because of commutativity property, a cascade
(sequence) of
SELECT operations may be applied in any order:
<cond1>(<cond2> (<cond3> (R)) = <cond2>
(<cond3> (<cond1> ( R)))
UNARY RELATIONAL OPERATIONS: SELECT
<attribute list>(R)
(pi) is the symbol used to represent the project
operation
<attribute list> is the desired list of attributes from
relation R.
The project operation removes any duplicate tuples
be a set of tuples
Mathematical sets do not allow duplicate elements.
ID NAME GRADE AGE
1 Alice A 20
2 Bob B 21
3 Alice A 20
4 Carol A 22
UNARY RELATIONAL OPERATIONS: PROJECT (CONTD.)
PROJECT Operation Properties
The number of tuples in the result of projection
<list>(R) is always less or equal to the number of tuples
in R
If the list of attributes includes a key of R, then the
number of tuples in the result of PROJECT is equal to
the number of tuples in R
PROJECT is not commutative
<list1> ( <list2> (R) ) = <list1> (R) as long as <list2>
ID NAME GRADE AGE
1 Alice A 20
2 Bob B 21
3 Alice A 20
4 Carlo A 22
If we apply:
π NAME, GRADE (STUDENT)
If we apply:
π NAME, GRADE (π NAME (STUDENT))
EXAMPLES OF APPLYING
SELECT AND PROJECT
OPERATIONS
RELATIONAL ALGEBRA EXPRESSIONS
We may want to apply several relational algebra
operations one after the other
Either we can write the operations as a single
relational algebra expression by nesting the
operations, or
We can apply one operation at a time and create
intermediate result relations.
In the latter case, we must give names to the relations
that hold the intermediate results.
SINGLE EXPRESSION VERSUS SEQUENCE OF RELATIONAL
OPERATIONS (EXAMPLE)
To retrieve the first name, last name, and salary of all employees
who work in department number 5, we must apply a select and a
project operation
We can write a single relational algebra expression as follows:
FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))
OR We can explicitly show the sequence of operations, giving a name
to each intermediate relation:
DEP5_EMPS DNO=5(EMPLOYEE)
RESULT FNAME, LNAME, SALARY (DEP5_EMPS)
ID NAME AGE GRADE
1 Alice 20 A
2 Bob 21 B
3 Carol 22 A
4 David 20 B
UNARY RELATIONAL OPERATIONS: RENAME
ρWORKER (EMPLOYEE)
The two operand relations R and S must be “type
compatible” (or UNION compatible)
R and S must have same number of attributes
Each pair of corresponding attributes must be type
compatible (have same or compatible domains)
RELATIONAL ALGEBRA OPERATIONS FROM SET THEORY:
UNION
Example:
To retrieve the social security numbers of all employees
who either work in department 5 (RESULT1 below) or directly
supervise an employee who works in department 5 (RESULT2
below)
We can use the UNION operation as follows:
R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are type compatible
if:
they have the same number of attributes, and
INTERSECTION is denoted by
The result of the operation 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”
RELATIONAL ALGEBRA OPERATIONS FROM SET THEORY: SET
DIFFERENCE (CONT.)
(R S) T = R (S T)
in a combinatorial fashion.
Denoted by R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm)
Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
The resulting relation state has one tuple for each
ACTUAL_DEPS
SSN=ESSN(EMP_DEPENDENTS)
RESULT FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)
RESULT will now contain the name of female employees
BINARY RELATIONAL OPERATIONS: JOIN
who
manages the department
The join condition can also be specified as
DEPARTMENT.MGRSSN= EMPLOYEE.SSN
RESULT OF THE JOIN OPERATION
EQUIJOIN.
BINARY RELATIONAL OPERATIONS: NATURAL JOIN
OPERATION
DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNUMBER
Another example: Q R(A,B,C,D) * S(C,D,E)
The implicit join condition includes each pair of attributes with
the
same name, “AND”ed together:
R.C=S.C AND R.D.S.D
Result keeps only one attribute of each such pair:
Example of NATURAL JOIN
operation
COMPLETE SET OF RELATIONAL OPERATIONS
R
<join condition>S = <join condition> (R
X S)
BINARY RELATIONAL OPERATIONS: DIVISION
DIVISION Operation
The division operation is applied to two relations
R(Z) S(X), where X subset Z. Let Y = Z - X (and hence Z=
X Y); that is, let Y be the set of attributes of R that are not
attributes of S.
The result of DIVISION is a relation T(Y) that includes a tuple
t if tuples tR appear in R with tR [Y] = t, and with
t [X] = t for every tuple t in S.
R s s
Query tree:
A tree data structure that corresponds to a relational algebra expression. It
represents the input relations of the query as leaf nodes of the tree, and
represents the relational algebra operations as internal nodes.
An execution of the query tree consists of executing an internal node
operation whenever its operands are available and then replacing that
internal node by the relation that results from executing the operation.
Query graph:
A graph data structure that corresponds to a relational calculus
expression. It does not indicate an order on which operations to perform
first. There is only a single graph corresponding to each query.
QUERY TREES AND QUERY GRAPH CORRESPONDING TO Q2