Relational Algebra
Б.Наранчимэг
Мэдээлэл, компьютерийн ухааны тэнхим
ХШУИС, МУИС
[email protected]
SQL
• Purpose and importance of SQL.
• How to retrieve data from database using SELECT and:
• Use compound WHERE conditions.
• Sort query results using ORDER BY.
• Use aggregate functions.
• Group data using GROUP BY and HAVING.
• Use subqueries.
Introduction
• Relational algebra is formal language associate with
the relational model.
• Informally, relational algebra is a (high-level)
procedural language and relational calculus a non-
procedural language.
• However, formally both are equivalent to one another.
Relational algebra (RA)
• Relational algebra operations work on one or more relations
to define another relation without changing the original
relations.
• Both operands and results are relations, so output from one
operation can become input to another operation.
• Allows expressions to be nested, just as in arithmetic. This
property is called closure.
Relational algebra
• Five basic operations in relational algebra:
• Selection
• Projection
• Cartesian product
• Union, and Set difference.
• These perform most of the data retrieval operations needed.
• Also have Join, Intersection, and Division operations, which
can be expressed in terms of 5 basic operations.
Relational algebra operations
Relational algebra operations
Selection (or Restriction)
condition(R)
• works on a single relation R and defines a relation that contains only
those tuples of R that satisfy the specified condition ( predicate).
Selection (or Restriction)
condition(R)
• works on a single relation R and defines a relation that contains only
those tuples of R that satisfy the specified condition ( predicate).
• The symbol σ (sigma) is used to denote the select operator.
• condition(R) нь “select * from R where condition”-тэй ижил.
Example 4.1 Selection operation
• List all staff with a salary greater than £10,000.
• σsalary > 10000(Staff)
Selection (or Restriction)
Logical operator
• ∧ (AND)
• ∨ (OR)
• ~ (NOT)
Projection
• col1, . . . , coln(R)
• works on a single relation R and defines a relation that contains a
vertical subset of R, extracting the values of specified attributes and
eliminating duplicates.
Projection
• col1, . . . , coln(R)
• works on a single relation R and defines a relation that contains a
vertical subset of R, extracting the values of specified attributes and
eliminating duplicates.
• col1, . . . , coln(R) нь “Select col1, …, coln from R”-тэй ижил.
Example 4.2 Projection operation
• Produce a list of salaries for all staff, showing only the staffNo, fName,
lName, and salary details.
• ΠstaffNo, fName, lName, salary(Staff)
Selection and Projection
• lname,fname, position,salary(sex=’F’(staff))
• Select lname, fname, position, salary from staff
where sex=‘F’
Union
•RS
• The union of two relations R and S defines a relation that contains all
the tuples of R, or S, or both R and S, duplicate tuples being
eliminated.
• R and S must be union-compatible.
• If R and S have I and J tuples, respectively, their union is obtained by
concatenating them into one relation with a maximum of (I + J)
tuples.
Example 4.3 Union operation
• List all cities where there is either a branch office or a property for
rent.
• Πcity(Branch) ∪ Πcity(PropertyForRent)
Set difference
•R-S
• The Set difference operation defines a relation consisting of the
tuples that are in relation R, but not in S.
• R and S must be union-compatible.
Example 4.4 Set difference
• List all cities where there is a branch office but no properties for rent.
• Πcity(Branch) − Πcity(PropertyForRent)
Intersection
RS
• The Intersection operation defines a relation consisting of the set of
all tuples that are in both R and S.
• R and S must be union-compatible.
• Expressed using basic operation.
• R S = R – (R - S)
Example 4.5 Intersection
• List all cities where there is both a branch office and at least one
property for rent.
• Πcity(Branch) ∩ Πcity(PropertyForRent)
Cartesian product
•R×S
• operation defines a relation that is the concatenation of every tuple
of relation R with every tuple of relation S.
• if one relation has I tuples and N attributes and the other has J tuples
and M attributes, the Cartesian product relation will contain (I * J)
tuples with (N + M) attributes
Example 4.6 Cartesian product
• List the names and comments of all clients who have viewed a
property for rent.
• (ΠclientNo, fName, lName(Client)) × (ΠclientNo, propertyNo, comment(Viewing))
Example 4.6 Cartesian product
Use selection operation to extract those tuples where Client.clientNo = Viewing.clientNo
σClient.clientNo = Viewing.clientNo((ΠclientNo, fName, lName(Client)) × (ΠclientNo, propertyNo, comment(Viewing)))
Cartesian product and Selection can be reduced to single operation called a JOIN
Join operations
• Join is a derivative of Cartesian product
• Equivalent to performing a Selection, using join predicate as
selection formula, over Cartesian product of the two
operand relations.
• One of the most difficult operations to implement efficiently
in an RDBMS and one reason why RDBMSs have intrinsic
performance problems.
Join operations
• Various forms of join operation
• Theta join
• Equijoin (a particular type of Theta join)
• Natural join
• Outer join
• Semijoin
Theta join (θ-join)
• R ⋈F S
• defines a relation that contains tuples satisfying the predicate F from
the Cartesian product of R and S.
• The predicate F is of the form R.ai θ S.bi where q may be one of the
comparison operators (<, ≤, >, ≥, =, ≠)
Theta join (θ-join)
• Theta join using basic Selection and Cartesian product operations.
R ⋈F S = F(R X S)
• Degree of a Theta join is sum of degrees of the operand relations R
and S. If predicate F contains only equality ( = ), the term Equijoin is
used.
Example 4.7 Equijoin operation
• List the names and comments of all clients who have viewed a
property for rent.
• (ΠclientNo, fName, lName(Client)) ⋈Client.clientNo = Viewing.clientNo
(ΠclientNo, propertyNo, comment(Viewing))
Natural join
•R⋈S
• Equijoin of the two relations R and S over all common attributes x.
One occurrence of each common attribute is eliminated from the
result.
Example 4.8 Natural join
• List the names and comments of all clients who have viewed a
property for rent.
• (ΠclientNo, fName, lName(Client)) ⋈ (ΠclientNo, propertyNo, comment(Viewing))
Outer join
• To display rows in the result that do not have matching values in the
join column, use Outer join.
R⟕S
• Left Outer join is a join in which tuples from R that do not have
matching values in the common attributes of S are also included in
the result relation.
• Missing values in the second relation are set to NULL.
Example 4.9 Left Outer join
• Produce a status report on property viewings.
• (ΠpropertyNo, street, city(PropertyForRent)) ⟕ Viewing
Outer joins
• Left Outer join
•R⟕S
• Right Outer join
•R⟖S
• Full Outer join
•R⟗S
Semijoin
• R ⊳F S
• defines a relation that contains the tuples of R that participate in the
join of R with S.
• Can rewrite Semijoin using Projection and Join
R ⊳F S = ΠA(R ⋈ S)
Example 4.10 Semijoin
• List complete details of all staff who work at the branch in Glasgow.
• Staff ⊳ Staff.branchNo = Branch branchNo(σcity = ‘Glasgow’ (Branch))
Assignment operation
• The assignment operation (←) provides a convenient way to express
complex queries.
• Staff1 salara>20000(Staff)
• RESULT lname, fname, position(Staff1)
Division
•R÷S
• defines a relation over the attributes C that consists of the set of
tuples from R that match the combination of every tuple in S.
• Expressed using basic operations
• T1←ΠC(R)
• T2←ΠC((T1 × S) − R)
• T ← T1 − T2
Example 4.11 Division
• Identify all clients who have viewed all properties with three rooms.
• (ΠclientNo, propertyNo(Viewing)) ÷ (ΠpropertyNo(σrooms = 3(PropertyForRent)))
Aggregation and Grouping
• GAL(R)
• Applies aggregate function list, AL, to R to define a relation over the
aggregate list.
• AL contains one or more (<aggregate_function>, <attribute>) pairs.
• Main aggregate functions are:
• COUNT
• SUM
• AVG
• MIN
• MAX
Example 4.12 Aggregate functions
• How many properties cost more than £350 per month to rent?
• ρR(myCount) G COUNT (propertyNo) (σrent > 350 (PropertyForRent))
• Find the minimum, maximum, and average staff salary.
• ρR(myMin, myMax, myAverage) G MIN(salary), MAX(salary), AVERAGE(salary) (Staff)
Grouping
• GAGAL(R)
• Groups tuples of R by grouping attributes, GA, and then applies
aggregate function list, AL, to define a new relation.
• a1, a2, . . . , an G <Ap ap>, <Aq aq>, . . . , <Az az> (R)
Example 4.13 Grouping operation
• Find the number of staff working in each branch and the sum of their
salaries.
• ρR(branchNo, myCount, mySum) branchNo G COUNT(staffNo), SUM(salary) (Staff)
Summary
Summary