IMS Lect10 12 RAlgebra
IMS Lect10 12 RAlgebra
Relational Calculus
i
Relational Algebra operations
• Relational Algebra consists of several groups of operations
• Unary Relational Operations
• SELECT (symbol: (sigma))
• PROJECT (symbol: (pi))
• RENAME (symbol: (rho))
• Relational Algebra Operations from Set Theory
• UNION ( ), INTERSECTION ( ), DIFFERENCE ( MINUS, – )
• CARTESIAN PRODUCT ( x )
• Binary Relational Operations
• JOIN (several variations of JOIN exist)
• DIVISION
• Additional Relational Operations
• OUTER JOIN, OUTER UNION
• AGGREGATE FUNCTIONS (These compute summary of
information: e.g. SUM, COUNT, AVG, MIN, MAX)
Relational Database
A relational database consists of following relations:
• Branch (branchNo, street, city, postcode)
• Staff (staffNo, fName, lName, position, sex, DOB, salary,
branchNo)
• PropertyForRent (propertyNo, street, city, postcode, type,
rooms, rent, ownerNo, staffNo, branchNo)
• Client (clientNo, fName, lName, telNo, prefType, maxRent,
eMail)
• PrivateOwner (ownerNo, fName, lName, address, telNo,
eMail, password)
• Viewing (clientNo, propertyNo, viewDate, comment)
• Registration (clientNo, branchNo, staffNo, dateJoined)
Selection (or Restriction)
• predicate (R)
• Works on a single relation R and defines a
relation that contains only those tuples (rows) of
R that satisfy the specified condition (predicate).
loan-number amount
L-14 1300
L-17 2000
L-12 1200
Exercise
• Given a relation
Customer(customer-name, customer-street, customer-city)
customer-name
Shreya
Kartik
Union
• RS
• 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, union is
obtained by concatenating them into one relation with a
maximum of (I + J) tuples.
customer-name
Shreya
Rahul
Kartik
Set Difference
• R–S
• Defines a relation consisting of the tuples
that are in relation R, but not in S.
• R and S must be union-compatible.
Example - Set Difference
• List all cities where there is a branch office
but no properties for rent.
Example - Set Difference
• List all cities where there is a branch office but
no properties for rent.
city(Branch) – city(PropertyForRent)
Find name of all customers who take a loan but are
not account holders
customer-name(borrower) - customer-name(depositor)
customer-name
Shreya
Murali
Intersection
• RS
• 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 operations:
R S = R – (R – S)
Example - Intersection
• List all cities where there is both a branch office
and at least one property for rent.
R FS = F(R S)
• R S
• An 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 - 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))
Division
• RS
• Defines a relation over the attributes C that
consists of set of tuples from R that match
combination of every tuple in S.
Division
Boats instance
Examples
Q1:Find names of sailors who’ve reserved boat #103
Q2:Find names of sailors who’ve reserved a red boat
Q3:Find names of sailors who’ve reserved a red or a green
boat
Q4:Find names of sailors who’ve reserved a red and a green
boat
Q5:Find the names of sailors who’ve reserved all boats
Find names of sailors who’ve reserved boat #103
Boats instance
Find sailors who’ve reserved a red and a
green boat
• Must identify sailors who’ve reserved red boats, sailors
who’ve reserved green boats, then find the intersection
(sid is a key for Sailors):
1 2 3 1 5 6
2 4 5 1 5 6
1 2 3 4 5 6
2 4 5 4 5 6
EQUI JOIN
A.A1 A2 A3 B.A1 B1 B2
1 2 3 1 5 6
A
A1 A2 A3 THETA JOIN
1 2 3 A.A1<B.A1
2 4 5 A.A1 A2 A3 B.A1 B1 B2
B 1 2 3 4 5 6
A1 B1 B2 2 4 5 4 5 6
1 5 6
4 5 6 NATURAL JOIN
A.A1 A2 A3 B1 B2
1 2 3 5 6
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 join in which tuples from R that
do not have matching values in common columns of
S are also included in result relation.
Semijoin
• R FS
• Defines a relation that contains the tuples of R that
participate in the join of R with S.
R FS = A(R F S)
A B
A1 A2 A3 A1 B1 B2
1 2 3 1 5 6
2 4 5 4 5 6
1 2 3 5 6 1 2 3 5 6
A FULL OUTER
A.A1 A2 A3 B1 B2
1 2 3 5 6
2 4 5 NULL NULL
4 NULL NULL 5 6
A A1 A2 A3
1 2 3
4 3 4
A NATURAL JOIN B
2 4 5
A1 A2 A3 B1 B2
B
A1 B1 B2 4 3 4 5 6
1 5 6
1 2 3 5 6
4 5 6
1 2 3 5 6
Aggregate Operations
• ℑAL (R)
• ℑ (Pronounced script F)
• Applies Aggregate List (AL) to R to define a relation over
the aggregate list.
• AL contains one or more
(<aggregate_function>, <attribute>) pairs.
• Some aggregate functions: COUNT, SUM, AVG, MIN,
MAX.
How many properties cost more than 350 per month
to rent?
myCount
5
• How many properties cost more than 350 per month
to rent?
• Select count(propertyNo)
from 𝑃𝑟𝑜𝑝𝑒𝑟𝑡𝑦𝐹𝑜𝑟𝑅𝑒𝑛𝑡
where rent >350
myCount
5
Grouping Operation
• GAℑAL (R)
• Groups tuples of R by grouping attributes (GA) and
then applies aggregate list (AL) to define a new relation.
• AL contains one or more
(<aggregate_function>, <attribute>) pairs.
• Resulting relation contains the grouping attributes GA
along with results of each of the aggregate functions.
Find the number of staff working in each branch
and the sum of their salaries.