Relational Algebra
Relational Algebra
• Example: To list each employee’s first and last name and salary, the
following is used:
LNAME, FNAME,SALARY(EMPLOYEE)
• The general form of the project operation is <attribute list>(R)
where (pi) is the symbol used to represent the project operation
and <attribute list> is the desired list of attributes from the attributes
of relation R.
• The project operation removes any duplicate tuples, so the result
of the project operation is a set of tuples and hence a valid
relation.
Rename Operation
• 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.
•
• 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))
The general Rename operation can be expressed by any of the following forms:
• S (B1, B2, …, Bn ) ( R) is a renamed relation S based on R with column names B1, B1,
…..Bn.
• S( R) is a renamed relation S based on R (which does not specify column names).
• (B1, B2, …, Bn ) ( R) is a renamed relation with column names B1, B1, …..Bn which does
STUDENTINSTRUCTOR
Type Compatibility for Set Operations
• Type Compatibility
▫ The operand relations R1(A1, A2, ..., An) and R2(B1,
B2, ..., Bn) must have the same number of attributes,
and the domains of corresponding attributes must be
compatible; that is, dom(Ai)=dom(Bi) for i=1, 2, ..., n.
INTERSECTION OPERATION
• The result of this operation, denoted by R S, is a relation
that includes all tuples that are in both R and S. The two
operands must be "type compatible"
• Example: The result of the intersection operation (figure
below) includes only those who are both students and
instructors.
Set Difference (or MINUS) Operation
• The result of this operation, denoted by R - S, is a relation that includes all
tuples that are in R but not in S. The two operands must be "type
compatible”.
• Example: The figure shows the names of students who are not instructors,
and the names of instructors who are not students.
STUDENT-INSTRUCTOR
INSTRUCTOR-
STUDENT
Set Operations Properties
• Notice that both union and intersection are commutative
operations; that is
R S = S R, and R S = S R
FEMALE_EMPS SEX=’F’(EMPLOYEE)
EMPNAMES FNAME, LNAME, SSN (FEMALE_EMPS)
Denoted by *, the standard definition of natural join requires that the two join
attributes, or each pair of corresponding join attributes, have the same name in
both relations. If this is not the case, a renaming operation is applied first.
Example
• To apply a natural join on the DNUMBER attributes of
DEPARTMENT and DEPT_LOCATIONS, it is sufficient to
write:
DEPT_LOCS DEPARTMENT * DEPT_LOCATIONS
Complete Set of Relational Operations
• The set of operations including select , project ,
union , set difference - , and cartesian product X
is called a complete set because any other relational
algebra expression can be expressed by a
combination of these five operations.
• For example:
R S = (R S ) – ((R - S) (S - R))
R <join condition> S = <join condition> (R X S)
Aggregate functions
Use of the Functional operator g
g MAX (Salary) (Employee) retrieves the maximum salary value from
the Employee relation
g MIN (Salary) (Employee) retrieves the minimum Salary value from
the Employee relation
g (Employee) retrieves the sum of the Salary from the
SUM (Salary)
Employee relation
g (Employee) groups employees by
DNO COUNT (SSN), AVERAGE (Salary)
• Relation borrower
• Inner join
b. 4 6
c. 4 6
5 6