Lec # 15,16 - Relational Algebra
Lec # 15,16 - Relational Algebra
Lec # 15,16 - Relational Algebra
Relational Algebra
• The basic set of operations for the relational model
is the relational algebra.
– enable the specification of basic retrievals
• The result of a retrieval is a new relation, which
may have been formed from one or more relations.
– algebra operations thus produce new relations, which
can be further manipulated the same algebra.
• A sequence of relational algebra operations forms
a relational algebra expression,
– the result will also be a relation that represents the
result of a database query (or retrieval request).
2
What is an Algebra?
• A language based on operators and a
domain of values
• Operators map values taken from the
domain into other domain values
• Hence, an expression involving operators
and arguments produces a value in the
domain
• When the domain is a set of all relations we
get the relational algebra
3
Relational Algebra Definitions
• Domain: set of relations
• Basic operators: select,
select project,
project union,
union set
difference,
difference Cartesian (cross) product
• Derived operators: set intersection,
intersection division,
division
join
• Procedural: Relational expression specifies
query by describing an algorithm (the sequence
in which operators are applied) for determining
the result of an expression
4
Unary Relational Operations
• SELECT Operation: used to select a subset
of the tuples from a relation that satisfy a selection
condition. It is a filter that keeps only those tuples
that satisfy a qualifying condition.
Examples:
DNO = 4 (EMPLOYEE)
SALARY > 30,000 (EMPLOYEE)
– denoted by <selection condition>(R) where the symbol
(sigma) is used to denote the select operator, and the
selection condition is a Boolean expression specified on
the attributes of relation R
5
SELECT Operation Properties
7
Select Examples
Person Id Name Address Hobby
1123 John 123 Main stamps
1123 John 123 Main coins
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps
NOT(Hobby=‘hiking’) (Person)
Person
Hobby‘hiking’ (Person)
8
Person
Unary Relational Operations (cont.)
• PROJECT Operation: selects certain columns
from the table and discards the others.
Example:
10
SELECT and PROJECT Operations
13
UNION Operation
Denoted by R S
Result is a relation that includes all tuples that are either
in R or in S or in both. Duplicate tuples are eliminated.
Example: Retrieve the SSNs of all employees who either
work in department 5 or directly supervise an employee
who works in department 5:
DEP5_EMPS DNO=5 (EMPLOYEE)
RESULT1 SSN (DEP5_EMPS)
RESULT2(SSN) SUPERSSN(DEP5_EMPS)
RESULT RESULT1 RESULT2
The union operation produces the tuples that are in either
RESULT1 or RESULT2 or both. The two operands must
be “type compatible”. 14
UNION Operation
Type (Union) Compatibility
15
Example
Tables:
Person (SSN, Name, Address, Hobby)
Professor (Id, Name, Office, Phone)
are not union compatible.
But
Name (Person)
Person and Name (Professor)
Professor
are union compatible so
Name (Person)
Person - Name (Professor)
Professor
16
makes sense.
UNION Example
STUDENT INSTRUCTOR:
18
Set Difference Example
S1 S2
SID SName Age SID SName Age
473 Popeye 22 202 Rusty 21
192 Jose 22 403 Marcia 20
715 Alicia 28 914 Hal 24
914 Hal 24 192 Jose 22
881 Stimpy 19
19
Relational Algebra Operations From
Set Theory (cont.)
• Union and intersection are commutative operations:
R S = S R, and R S = S R
A B C D A B C D
x1 x2 y1 y2 x1 x2 y1 y2
x3 x4 y3 y4 x1 x2 y3 y4
x3 x4 y1 y2
R S x3 x4 y3 y4
21
R S
Cartesian Product Example
DEPT_MGR
DEPARTMENT MGRSSN=SSN EMPLOYEE
EQUIJOIN & NATURAL JOIN
• EQUIJOIN
– most common join: join conditions with equality
comparisons only.
– in the result of an EQUIJOIN we always have one or
more pairs of attributes (whose names need not be
identical) that have identical values in every tuple.
– The JOIN in the previous example was EQUIJOIN.
• NATURAL JOIN
– Because one of each pair of attributes with identical
values is superfluous, a new operation called natural
join—denoted by *—was created to get rid of the
second (superfluous) attribute.
– The standard definition of natural join requires that each
pair of corresponding join attributes, have the same
name in both relations. If this is not the case, a
renaming operation is applied. 27
Natural Join Operations
(a) PROJ_DEPT PROJECT * DEPT
(b) DEPT_LOCS DEPARTMENT *
DEPT_LOCATIONS
The DIVISION Operation
(a) Dividing SSN_PNOS by SMITH_PNOS.
(b) T R ÷ S.
Additional Relational
Operations
• Aggregate Functions and Grouping
• Recursive Closure Operations
• The OUTER JOIN Operation
Aggregate Functions
E.g. SUM, AVERAGE, MAX, MIN, COUNT
Recursive Closure Example
OUTER JOINs
• In NATURAL JOIN tuples without a matching (or related) tuple
are eliminated from the join result. Tuples with null in the join
attributes are also eliminated. This loses information.
• Outer joins, can be used when we want to keep all the tuples in R,
all those in S, or all those in both relations
– regardless of whether they have matching tuples in the other relation.
• 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 “padded” with null values.
• 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. 33
Left Outer Join
E.g. List all employees and the department
they manage, if they manage a department.
Examples of Queries in
Relational Algebra
• Work through Query 1 (p. 171) and Query
2 (p. 172) of E&N
• Show all intermediate relations as well as
the final result