Professional Documents
Culture Documents
DB Ch08 Elmas - 7 Ed
DB Ch08 Elmas - 7 Ed
The Relational
Algebra and
Calculus
Prepared By
Dr. Alaa Hosni
• Historically, the relational algebra and calculus were developed
before the SQL language.
• SQL is based on concepts from both the algebra and the calculus .
Chapter 8-2
⚫ Relational algebra : the basic set of operations for the
relational model .
Chapter 8-3
⚫ SQL describes the practical language for the
relational model.
⚫ SQL is based on concepts from both the algebra
and the calculus.
Chapter 8-4
⚫ Importance of relational algebra:
1. It provides a formal foundation for relational model operations.
2. It is used as a basis for implementing queries
3. Optimizing the query processing (optimization modules-
integral parts RDBMSs)
Chapter 8-5
Manages
Works_for
controls
DEPENDENT_OF
Example
To select the EMPLOYEE tuples whose department number is four:
DNO=4 (EMPLOEE)
Chapter 8-7
⚫ The selection condition is a Boolean expression specified
on the attributes of relation R
⚫ All the comparison operators in the set {=, <, ≤, >, ≥, ≠} can
apply to attributes whose domains are ordered values, such as
– numeric or
– date domains.
– Domains of strings of characters
Chapter 8-8
⚫ If the domain of an attribute is a set of unordered values,
then only the comparison operators in the set {=, ≠ } can be
used.
Example
The following set is an unordered domain
Color = { ‘red’, ‘blue’, ‘green’, ‘white’, ‘yellow’, ...}, where no
order is specified among the various colors.
⚫ Additional types of comparison operators:
A domain of character strings may allow the comparison
operator SUBSTRING_OF.
Chapter 8-9
⚫ Standard Boolean operators and, or, and not can be
used to form a general selection condition.
Example
To select the tuples for all employees who either work in
department 4 and make over $25,000 per year, or work in
department 5 and make over $30,000, we can specify the
following SELECT operation:
σ(Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)(EMPLOYEE)
Chapter 8-10
Chapter 8-11
⚫ The SELECT operator is unary: applied to a single relation.
Chapter 8-12
❑SELECT operation may be applied in any order; i.e.,
<condition1>( < condition2> ( <condition3> ( R))
= <condition2> ( < condition3> ( < condition1> (R)))
Chapter 8-13
8.1.2 The PROJECT Operation
⚫ This operation selects certain columns from the table
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)
<attribute list> : the desired list of attributes from R.
Chapter 8-14
Chapter 8-15
PROJECT Operation Properties
Chapter 8-16
8.1.3 Sequences of Operations and the RENAME Operation
Chapter 8-17
Example
To retrieve the Fname, Lname, and salary of all employees who
work in Dnumber 5, we can write a single relational algebra
expression as follows:
FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))
Chapter 8-18
OR We can explicitly show the sequence of operations, giving a
name to each intermediate relation:
D5_EMPS DNO=5(EMPLOYEE)
RESULT FNAME, LNAME, SALARY (D5_EMPS)
Chapter 8-19
⚫ The rename operator is
⚫ can be used as follows:
1) S (B1, B2, …, Bn ) ( R)
where S is the new relation based on R, B1, B1, …..Bn are the
new attribute names
2) S (R) is a renamed relation S based on R (which does not
specify column names).
3) (B1, B2, …, Bn ) ( R) is a renamed relation with column names
B1, B1, …..Bn (the same relation name).
Chapter 8-20
⚫In SQL,
SELECT E.Fname AS First_name, E.Lname AS
Last_name, E.Salary AS Salary
FROM EMPLOYEE AS E
WHERE E.Dno=5;
Chapter 8-21
8.2 Relational Algebra Operations From Set Theory
Chapter 8-22
⚫ Assume that the relations R and S are union-compatible
⚫ We can define the three operations UNION,
INTERSECTION, and SET DIFFERENCE as follows:
■ R ∪ S, is a relation that includes all tuples that are either in R or in S or
in both R and S.
Duplicate tuples are eliminated.
■ R ∩ S, is a relation that includes all tuples that are in both R and S.
■ R – S, is a relation that includes all tuples that are in R but not in S.
Chapter 8-23
Example
D5_EMPS DNO=5 (EMPLOYEE)
RESULT1 SSN(D5_EMPS)
RESULT2 (SSN) SUPERSSN(D5_EMPS)
RESULT RESULT1 RESULT2
RESULT : the SSN of all employees who either work in department 5 or directly
supervise an employee who works in department 5.
Chapter 8-24
Chapter 8-25
⚫ Notice that
R ∪S=S∪R,R∩S=S∩R
R ∪ (S ∪ T ) = (R ∪ S ) ∪ T
(R ∩ S ) ∩ T = R ∩ (S ∩ T )
Chapter 8-26
Chapter 8-27
8.2.2 The CARTESIAN PRODUCT Operation
⚫ CARTESIAN (or cross product) Operation
– The result of R(A1, A2, . . ., An) S(B1, B2, . . ., Bm) is a
relation Q with degree n + m attributes
Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
Chapter 8-28
⚫ Suppose that we want to retrieve a list of names of each
female employee’s dependents:
Example
FEMALE_EMPS SEX=’F’(EMPLOYEE)
EMPNAMES FNAME, LNAME, SSN (FEMALE_EMPS)
EMP_DEPENDENTS EMPNAMES DEPENDENT
ACTUAL_DEPENDENTS ← σSsn=Essn(EMP_DEPENDENTS)
Chapter 8-29
Chapter 8-30
Chapter 8-31
Chapter 8-32
8.3 Binary Relational Operations: JOIN and DIVISION
8.3.1 JOIN Operation
⚫ The JOIN operation : the sequence of Cartesian product
followed by select is called JOIN.
Chapter 8-33
Example
⚫ To retrieve the name of the manager of each department:
– combine each DEPARTMENT tuple with the EMPLOYEE tuple
whose SSN value matches the MGRSSN value in the department
tuple. We do this by using the join operation.
DEPT_MGR DEPARTMENT MGRSSN=SSN
EMPLOYEE
RESULT← Dname,Fname, Lname (DEPT_MGR)
(EQUIJOIN: '=')
Chapter 8-34
⚫ General join condition of is of the form
<condition> AND <condition> AND...AND <condition>
<condition> is of the form Ai Bi ,
Ai is an attribute of R,
Bi is an attribute of S,
Ai and Bi have the same domain, and
is one of the comparison operators {=, <, , , >, , }.
⚫ A JOIN operation with such a general join condition is called a
THETA JOIN.
⚫ Tuples whose join attributes are NULL or for which the join
condition is FALSE do not appear in the result.
Chapter 8-35
8.3.2 Variations of JOIN: The EQUIJOIN
and NATURAL JOIN
Chapter 8-36
⚫ Suppose we want to combine each PROJECT tuple with the
DEPARTMENT tuple that controls the project.
⚫ First we rename the Dnumber attribute of DEPARTMENT to
Dnum :
Or
DEPT ← ρ(Dname, Dnum, Mgr_ssn, Mgr_start_date)(DEPARTMENT)
PROJ_DEPT ← PROJECT DEPT
Chapter 8-37
Chapter 8-38
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
Chapter 8-39
8.3.3 Complete Set of Relational Operations
⚫ The set of operations { , , , , - , } is called a complete set
because any other relational algebra expression can be
expressed by a combination of these five operations.
Example
1. R S = (R S ) – ((R - S) (S - R))
2. R <join condition>S = <join condition> (R S)
3. NATURAL JOIN can be specified as a CARTESIAN
PRODUCT preceded by RENAME and followed by SELECT
and PROJECT operations.
Chapter 8-40
8.4 Additional Relational Operations
8.4.1 Generalized Projection
⚫ The generalized projection operation allows functions of attributes
to be included in the projection list.
Chapter 8-45
Example
Consider the relation
EMPLOYEE (Ssn, Salary, Deduction, Years_service)
Chapter 8-46
⚫ Then a generalized projection combined with renaming may
be used as follows:
Chapter 8-47
8.4.2 Aggregate Functions and Grouping
• Such functions include retrieving the average or total salary of
all employees or the total number of employee tuples.
Chapter 8-48
⚫ AGGREGATE FUNCTION operation is defined as
<grouping attributes> ℑ <function list> (R )
Chapter 8-49
Example
⚫ To retrieve each department number, the number of employees in
the department, and their average salary, while renaming the
resulting attributes as indicated below:
Chapter 8-50
⚫ If no grouping attributes are specified:
– the functions are applied to all the tuples
– the resulting relation has a single tuple only.
Chapter 8-51
Chapter 8-52
8.4.4 OUTER JOIN Operations
▪ In NATURAL JOIN
▪ tuples without a matching tuple are eliminated from the join result.
▪ Tuples with null in the join attributes are also eliminated.
▪ outer joins can be used when we want to keep all the tuples in R, or all
those in S, or all those in both relations in the result of the join .
▪ The left outer join operation keeps every tuple in the first or left relation R in
R S; if no matching tuple is found in S, then the attributes of S in the join
result are filled or “padded” with null values.
Chapter 8-54
Suppose that we want a list of all employee names as well as the
name of the departments they manage if they happen to manage
a department; if they do not manage one, we can indicate it with a
NULL value
Chapter 8-55
⚫ A similar operation, right outer join, keeps every tuple in
the second or right relation S in the result of R S.
⚫ A third operation, full outer join, denoted by keeps all
tuples in both the left and the right relations when no
matching tuples are found, padding them with null values
as needed.
Chapter 8-56
8.5 Examples of Queries in Relational Algebra
⚫ Q1: Retrieve the name and address of all employees who work for the
‘Research’ department.
RESEARCH_DEPT DAME=‘Research’ (DEPARTMENT)
RESEARCH_EMPS (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE EMPLOYEE)