Relational Algebra
Relational Algebra
Relational Algebra
Query Algebra
Relational Algebra
Set operators: Union, intersection, difference; in SQL, UNION, INTERSECT and EXCEPT
Selection; in SQL a select with optional where clause
Projection: chooses columns; in SQL specify them in a select, rename them, and compute
them
Product: in SQL a join with no conditions
Join: in SQL several types of joins
Extension operators
Elimination of duplicates: turns a bag into a set; in SQL use distinct in the select clause
Grouping: in SQL use GROUP BY and aggregate functions
Sorting: in SQL use ORDER BY
An algebra of bags
R ⋃ S : a tuple is in the result the number of times it is in R plus the number of times in S
R ∩ S : a tuple is in the result the minimum of times it is in R and in S
R – S : a tuple is in the result the number of times it is in R minus the times it is in S, but not
fewer than 0 times
R and S must have the same schema
In SQL
Projection operator
Product operator
R × S : the schema of the resulting relation has all attributes from R plus all from S (qualified
if they have the same name)
A tuple in the result is formed by a tuple of R concatenated by a tuple of S
And this for every tuple of R
Join operator
R ⋈ S : equivalent to πL ( sc (R x S))
Natural join: compares common columns of both tables using a condition c
o Should make sure common columns exist
o Projects only one of each equated attribute
Cartesian join: the product of two tables
θ-join: or equi-join if θis the = operator
o The equi-join comparing two attributes is the most common form of join (also called
inner join)
o Equated columns appear in the result
Renaming
ρ(R)
Used to rename result relations or columns
ρects ← credits(course)
Duplicate elimination
Sorting operator
τL (R)
Converts a bag to a list (implies ordering)
The tuples in relation R are sorted according to the sorting attributes (in ascending order by
default)
In SQL, ORDER BY the attributes in the list L
To order by descending order use the keyword DESC
If another operator is applied next, order is lost
Grouping
γL (R)
Aggregates tuples within a relation into subsets
Aggregation uses a function f in a single attribute (min, max, count, sum, avg)
The elements in L are either:
o A grouping attribute
o An aggregation function
Outer joins
Outer joins allow dangling tuples (no matching pair) to appear in the result by padding them
with ꓕ(NULL) values
Left outer joins keep dangling tuples from the left relation
Right outer joins keep dangling tuples from the right relation
Semi-join
R ⋉ S and R ⋊ S
Join and keep only the schema at left (right)
Anti-joins
R⊳S
Returns tuples of R having no matching in S
Also known as anti-semi-join, can be writen as
R⊳S=R−R⋉S
Division
R÷S
Returns tuples of R with a match in all tuples of S There is no corresponding SQL operator,
but there are several alternatives
Can answer questions with “give all tuples that match a given condition”
Can be translated as:
Properties of the operators
Natural join, multiplication, union and intersction are associative and commutative
o R ⋈ S ≡ R ⋈ S, (R ⋈ S) ⋈ T ≡ R ⋈ (S ⋈ T)
o R × S ≡ R × S, (R × S) × T ≡ R × (S × T)
o R ⋃ S ≡ R ⋃ S, (R ⋃ S) ⋃ T ≡ R ⋃ (S ⋃ T)
o R ∩ S ≡ R ∩ S, (R ∩ S) ∩ T ≡ R ∩ (S ∩ T)
Selection is commutative:
o σ1 (σ 2 (R)) ≡ σ 2 (σ 1 (R))
Difference is neither associative nor commutative
Teta join may not be associative (natural join is)
Equivalence rules
Expression trees
Example tree
Transformations