rELATIONAL Algebra
rELATIONAL Algebra
1
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
2
Relational Algebra Definitions
Domain: set of relations
Basic operators: select, project, union, set
difference, Cartesian (cross) product
Derived operators: set intersection, 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
3
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
4
SELECT Operation Properties
The SELECT operation <selection condition>(R) produces a
relation S that has the same schema as R
5
Selection Condition
Operators: <, , , >, =,
Simple selection condition:
◦ <attribute> operator <constant>
◦ <attribute> operator <attribute>
◦ <condition> AND <condition>
◦ <condition> OR <condition>
◦ NOT <condition>
6
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)
Hobby‘hiking’ (Person)
7
Unary Relational Operations (cont.)
PROJECT Operation: selects
certain columns from the
table and discards the others.
Example:
(EMPLOYEE)
LNAME, FNAME,SALARY
9
SELECT and PROJECT Operations
(a) (DNO=4 AND SALARY>25000) OR (DNO=5 AND
Row in project<=row
SALARY>30000)(EMPLOYEE) in Employee
(b) LNAME, FNAME, SALARY(EMPLOYEE)
(c) SEX, SALARY(EMPLOYEE)
For employees working in DNO=5LIST
THE FNAME,SSN,AND SALARY
𝑅1 ϭ𝐷𝑁𝑂=5 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒
ɲ 𝐹𝑁𝐴𝑀𝐸,𝑆𝑆𝑁,𝑆𝐴𝐿𝐴𝑅𝑌 (𝑅1 )
Relational Algebra Operations
from Set Theory
The UNION, INTERSECTION, and MINUS Operations
The CARTESIAN PRODUCT (or CROSS PRODUCT) Operation
Set Operators
A relation is a set of tuples, so set operations apply:
, , − (set difference)
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) and Name (Professor)
are union compatible so
Name (Person) - Name (Professor)
makes sense.
16
UNION Example
STUDENT INSTRUCTOR:
17
Set Difference Operation
Set Difference (or MINUS) Operation
20
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
21
Relational Algebra Operations From Set
Theory (cont.)
Union and intersection are commutative operations:
R S = S R, and R S = S R
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.