Relational Algebra Queries
Relational Algebra Queries
The key fields are underlined, and the domain of each field is listed after the field
name. Therefore sid is the key for Suppliers, pid is the key for Parts, and sid and pid
together form the key for Catalog. The Catalog relation lists the prices charged for
parts by Suppliers. Write the following queries in relational algebra.
2. Find the sids of suppliers who supply some red or green part.
3. Find the sids of suppliers who supply some red part or are at 221 Packer Street.
4. Find the sids of suppliers who supply some red part and some green part.
7. Find the sids of suppliers who supply every red or green part.
8. Find the sids of suppliers who supply every red part or supply every green part.
9. Find pairs of sids such that the supplier with the first sid charges more for some part than
the supplier with the second sid.
10. Find the pids of parts supplied by at least two different suppliers.
11. Find the pids of the most expensive parts supplied by suppliers named Yosemite Sham.
Consider the following schema:
The key fields are underlined, and the domain of each field is
listed after the field name. Therefore sid is the key for
suppliers, pid is the key for Parts, and sid and pid together
form the key for Catalog. The Catalog relation lists the prices
charged for parts by Suppliers. Write the following queries in
relational algebra.
1. Find the names of suppliers who supply some red part.
Catalog
Parts
σcolor=_’red’_Parts gives us
πpidσcolor=_’red’_Parts gives us
PID
1
2
5
SID
1
2
πsid((πpidσcolor=_’red’_Parts) Catalog) Suppliers) gives us:
Gives us:
Sname
Yosemite Sham
Wiley E. Coyote
2. Find the sids of suppliers who supply some red or
green part.
4. Find the sids of suppliers who supply some red part and
some green part.
Given:
Parts
Catalog
PID
1
2
3
4
5
SID PID
1 1
1 2
1 3
1 4
1 5
2 1
2 3
2 5
3 1
Asks the question – what sids in catalog contain all the part
numbers in the divisor. There is only one sid that has all the
part numbers, 1.
Catalog
SID PID Cost
1 1 $10.00
1 2 $20.00
1 3 $30.00
1 4 $40.00
1 5 $50.00
2 1 $9.00
2 3 $34.00
2 5 $48.00
3 1 $11.00
Parts
PID Pname Color
1 Red1 Red
2 Red2 Red
3 Green1 Green
4 Blue1 Blue
5 Red3 Red
Catalog
SID PID Cost
1 1 $10.00
1 2 $20.00
1 3 $30.00
1 4 $40.00
1 5 $50.00
2 1 $9.00
2 3 $34.00
2 5 $48.00
3 1 $11.00
Parts
PID Pname Color
1 Red1 Red
2 Red2 Red
3 Green1 Green
4 Blue1 Blue
5 Red3 Red
ρ(R1,Catalog)
ρ(R2,Catalog)
πR1.sid,R2.sid(σR1.pid=R2.pid ∧ R1.sid != R2.sid ∧ R1.cost > R2.cost (R1 × R2))
Catalog:
SID SID
1 2
3 1
3 2
10. Find the pids of parts supplied by at least two
different suppliers.
ρ(R1,Catalog)
ρ(R2,Catalog)
πR1.pid σR1.pid=R2.pid ∧ R1.sid != R2.sid (R1 × R2)
Suppliers
and Catalog:
ρ(R2,R1 Catalog)
Gives us:
R3 x R2
Gives us: