4 Ex Relalg
4 Ex Relalg
Fall-Winter 2009/10
Exercises
Michail Kazimianec
Werner Nutt
Damiano Somenzi
4. Relational Algebra
Consider the following schema:
Supplier(sid: integer, sname: string, address: string)
Part(pid: integer, pname: string, colour: string)
Catalog(sid: integer, pid: integer, cost: real)
The relation Supplier stores suppliers and the key of that relation is sid. The relation
Part stores parts, and pid is the key of that relation. Finally, Catalog stores which
supplier supplies which part at which cost. The key is the combination of the two
attributes sid and pid.
1. Write queries in relational algebra
Write the following queries in relational algebra.
1. Find the names of suppliers who supply some red part.
2. Find the IDs of suppliers who supply some red or green part.
3. Find the IDs of suppliers who supply some red part or are based at 21 George
Street.
4. Find the names of suppliers who supply some red part or are based at 21 George
Street.
5. Find the IDs of suppliers who supply some red part and some green part.
(Hint: use intersection of relations or join the same relation several times)
6. Find pairs of IDs such that the supplier with the first ID charges more for some
part than the supplier with the second ID.
(Hint: you may want to create temporary relations to get two copies of Catalog)
7. Find the IDs of suppliers who supply only red parts.
(Hint: A supplier supplies only red parts if it is not the case that the supplier
offers a part that is not red. This question is a challenge!)
8. Find the IDs of suppliers who supply every part.
(Hint: A supplier supplies every part if it is not the case that there is some part
which they do not supply. Use set difference and cartesian product. This question
is a challenge, too!)
References
Most of these exercises are taken from the book Database Management Systems by
Ramakrishnan/Gehrke, Chapter 4.