
University of Toronto
CSC343H1S Winter 2011
Relational Algebra Exercises for Tutorial
Solve all queries below using only select, project, Cartesian product, and natural join. Do not use
theta-join, set operations, renaming or assignment.
First Schema
Suppliers(sID, sName, address)
Parts(pID, pName, colour)
Catalog(sID, pID, price)
Catalog[sID] ⊆Suppliers[sID]
Catalog[pID] ⊆Parts[pID]
Notice:
•In this schema, everywhere we want values to match across relations, the attributes have
matching names. And everywhere the attributes have matching names, we want values to
match across relations.
•This means that natural join will do exactly what we want in all cases.
Questions:
1. If sID is a key for the Suppliers relation, could it be a key for the Catalog relation?
Answer: Just because it is a key in one relation doesn’t mean it is in another; being a key is
relative to the relation. But is it a key for Catalog? No. We almost surely want to be able
to list multiple parts by one supplier in our catalog.
2. Find the names of all red parts.
Answer:
ΠpName (σcolour=“red”P ar ts)
3. Find all prices for parts that are red or green. (A part may have different prices from different
manufacturers.)
Answer:
Πprice((σcolour =“red”∨colour=“green”P ar ts)./ Catalog)
4. Find the sIDs of all suppliers who supply a part that is red or green.
Answer:
ΠsID ((σcolour=“red”∨colour =“green”P arts)./ Catalog)
5. Find the sIDs of all suppliers who supply a part that is red and green.
Answer: Trick question. Each tuple has only one colour, and each part has only one tuple
(since pID is a key), so no part can be recorded as both red and green.
6. Find the names of all suppliers who supply a part that is red or green.
Answer:
ΠsName ((ΠsID ((σcolour=“red”∨colour =“green”P arts)./ C atalog)) ./ Suppliers)
1