Relational-algebra exercises
Appendix to Lecture 3
Running example: Movies database
Movie ( title, year, length, inColor, studioName, producerC)
MovieStar (name, address, gender, birthdate)
StarsIn (movieTitle, movieYear, starName)
MovieExec (name, address, cert, netWorth)
Studio (studioname, presc);
Movies
SIMPLE QUERIES
Selections: Movies
1. Find titles of all black-and-white movies which were
produced after 1970
2. Find titles of all movies produced by MGM studio
after 1970 or with length less than 1.5 hours
3. Find producer of ‘Star wars’
Projections: Movies
4. Info about all Disney movies produced in year 1990
5. Title and length of all Disney movies produced in year
1990
6. Title and length in hours of all Disney movies produced
in year 1990
Joins: Movies
7. For each movie’s title produce the name of this
movie’s producer
8. Find the names of producers of movies where
Harrison Ford starred.
Movie ( title, year, length, inColor, studioName, producerC)
MovieStar (name, address, gender, birthdate)
StarsIn (movieTitle, movieYear, starName)
MovieExec (name, address, cert, netWorth)
Studio (studioname, presc);
9. Find all name pairs in form (movie star, movie producer)
that live at the same address.
Star=ρstar,staraddress (πname, address (MovieStar))
Prod=ρprod, prodaddress (πname, address (MovieExec))
πstar,prod((Star)⋈staraddress=prodaddress AND star !=prod(Prod))
Movies
MORE COMPLEX QUERIES
Movie ( title, year, length, inColor, studioName, producerC)
MovieStar (name, address, gender, birthdate)
StarsIn (movieTitle, movieYear, starName)
MovieExec (name, address, cert, netWorth)
Studio (studioname, presc);
10. Find the names of all producers who did NOT produce ‘Star wars’
Simple:
πname(MovieExec) –
πname((Movie)⋈title=‘Star wars’ AND producerC=cert(MovieExec))
More efficient (smaller Cartesian product)
πname((σtitle=‘Star wars’(Movie))⋈producerC!=cert(MovieExec))
**9B. Find all name pairs in form (movie star, movie producer)
that live at the same address. Now, try to eliminate palindrome
pairs: leave (a,b) but not both (a,b) and (b,a).
1. Star=ρname→star(MovieStar)
Prod=ρname→prod(MovieExec)
2. Pairs = πstar,prod((Star)⋈Star.address=Prod.address AND
star!=prod(Prod))
3. PA = σstar<prod(Pairs) // Pairs in Ascending order
PD = σstar>prod(Pairs) //Pairs in Descending order Example on
the next page
4. Palindrome = (PA)⋈PA.star=PD.prod AND PA.prod=PD.star (PD)
5. Pairs – πPD.star,PD.prod (Palindrome)
1. Renaming
Star Prod
star addr prod addr
1
A 1 A 1 Star=ρname→star(MovieStar)
B 1 B 1 Prod=ρname→prod(MovieExec)
C 2 D 2
F 3 E 3
Star Addr Prod Addr 2. Cartesian
A 1 A 1
A 1 B 1 product:
A
A
1
1
D
E
2
3
Star x Prod
B 1 A 1
B 1 B 1 2. Pairs = πstar,prod
((Star)
B 1 D 2
⋈Star.address=Prod.address AND star!=prod
B 1 E 3 (Prod))
C 2 A 1
C 2 B 1 Pairs
C 2 D 2 Star Prod
C 2 E 3 A B
F 3 A 1 B A
F 3 B 1 C D
F 3 D 2 F E
F 3 E 3
3. Sorted pairs
Pairs
Star Prod 3. PA = σstar<prod(Pairs) // Pairs in Ascending
PD = σstar>prod(Pairs) //Pairs in Descending
A B
B A
C D
F E
PA PD
Star Prod Star Prod
A B B A
C D F E
4. Cartesian product PA x PD
Palyndrome (only colored tuple qualify)
PA PD PA.Star PA.Prod PD.Star PD.Prod
Star Prod Star Prod A B B A
x
A B B A A B F E
C D F E C D B A
C D F E
4. Palindrome = (PA)
⋈PA.star=PD.prod AND PA.prod=PD.star
(PD)
5. Remove palindrome tuples from
pairs
5. Pairs – πPD.star,PD.prod (Palindrome)
Pairs
Star Prod
Palyndrome
A B PA.Star PA.Prod PD.Star PD.Prod
B A - A B B A
C D
F E result
Star Prod
A B
C D
F E
Movie ( title, year, length, inColor, studioName, producerC)
MovieStar (name, address, gender, birthdate)
StarsIn (movieTitle, movieYear, starName)
MovieExec (name, address, cert, netWorth)
Studio (studioname, presc);
11. Find names of producers that produced at least one
movie for each of different studios: Disney and MGM
πname[(σstudioName=‘Disney’(Movie))⋈producerC=cert(MovieExec)]
∧
πname[(σstudioName=‘MGM’(Movie))⋈producerC=cert(MovieExec)]
Movie ( title, year, length, inColor, studioName, producerC)
MovieStar (name, address, gender, birthdate)
StarsIn (movieTitle, movieYear, starName)
MovieExec (name, address, cert, netWorth)
Studio (studioname, presc);
12. Find all movie titles for which there is no producer entry
in MovieExec table
πtitle(Movie) – πtitle ((Movie)⋈producerC=cert(MovieExec))
Movie ( title, year, length, inColor, studioName, producerC)
MovieStar (name, address, gender, birthdate)
StarsIn (movieTitle, movieYear, starName)
MovieExec (name, address, cert, netWorth)
Studio (studioname, presc);
13. Find the names of all stars which starred in at least 2
movies (according to our database)
1. S1=ρtitle1,year1,name1(StarsIn)
S2=ρtitle2,year2,name2(StarsIn)
2. (S1) ⋈name1=name2 AND (title1 != title2 or year1!=year2)(S2)
Lab database: Pizza
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
Pizza
TEST YOURSELF ON SIMPLE
QUERIES
Projections: Pizza
1. Find full information about all possible places
and prices to get mushroom or pepperoni pizzas
2. Find name of pizzerias that serve mushroom or
pepperoni pizzas
3. Compute the full list of pizza types, with the
corresponding pizzerias and the price of pizza in
cents
Selections: Pizza
4. Find names of all customers under 18
5. Find names of all female customers older than
25
Join: Pizza
6. Find all pizza types that both Amy and Dan eat
7. Find the names of all females who eat a
mushroom pizza
8. Find the names of pizzerias where Hil can buy
pizzas she eats for less than 10$
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
9. Find the names of all females who eat either
mushroom or pepperoni pizza (or both).
πname(
σgender='female' AND (pizza='mushroom' OR pizza='pepperoni')(Person⋈Eats)
)
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
10. Find the names of all females who eat both
mushroom and pepperoni pizza.
πname(σgender='female' AND pizza='mushroom'(Person⋈Eats))
∩
πname(σgender='female' AND pizza='pepperoni'(Person⋈Eats))
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
11. Find all pizzerias that serve at least one pizza
that Amy eats for less than $10.00.
πpizzeria(σname='Amy'(Eats)⋈σprice<10(Serves))
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
12. Find all pizzerias frequented by at least one
person under the age of 18.
πpizzeria(σage<18(Person)⋈Frequents)
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
13. Find all pizza types which are not eaten by
anyone
πpizza(Serves) - πpizza(Eats)
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
14. Find all pizzerias that are frequented by only
females or only males.
πpizzeria(σgender='female'(Person)⋈Frequents) −
πpizzeria(σgender='male'(Person)⋈Frequents)
⋃
πpizzeria(σgender='male'(Person)⋈Frequents) −
πpizzeria(σgender='female'(Person)⋈Frequents)
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
15. Find all pizzerias where Dan could buy pizzas
that he eats, and where he has never bought a pizza
yet
πpizzeria[(σname=‘Dan'(Eats))⋈ (Serves) ]
-
π pizzeria (σname=‘Dan'(Frequents))
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
16. For each person, find all pizzas the person
eats that are not served by any pizzeria the
person frequents. Return all such person (name)
/ pizza pairs.
Eats−πname,pizza(Frequents⋈Serves)
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
17. Find the names of all people who frequent
only pizzerias serving at least one pizza they eat.
Explanation
πname(Person) on the next
page
−
πname(Frequents − πname,pizzeria(Eats⋈Serves))
17. Find the names of all people who frequent only
pizzerias serving at least one pizza they eat.
1. List of all pizzerias which serve at least one of pizzas
which particular person can eat:
πname,pizzeria(Eats⋈Serves)
2. List of all pizzerias which are frequented by this
person but do not serve any pizza he can it
Frequents - πname,pizzeria(Eats⋈Serves)
3. Answer to the query
πname(Person)
−
πname(Frequents − πname,pizzeria(Eats⋈Serves))
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
18. Find the names of all people who frequent
every pizzeria serving at least one pizza they eat.
Explanation
on the next
πname(Person) page
−
πname(πname,pizzeria(Eats⋈Serves)−Frequents)
18. Find the names of all people who frequent every pizzeria
serving at least one pizza they eat.
1. List of all pizzerias per person which serve at least one pizza
this person can eat:
πname,pizzeria(Eats⋈Serves)
2. List of pizzerias which serve the desirable pizza but which
person did not visit yet
πname,pizzeria(Eats⋈Serves)−Frequents
3. All the people excluding those in p.2
πname(Person)
−
πname(πname,pizzeria(Eats⋈Serves)−Frequents)
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )
19. Find the pizzeria serving the cheapest pepperoni
pizza. In the case of ties, return all of the cheapest-
pepperoni pizzerias.
πpizzeria(σpizza='pepperoni'Serves)
−
π pizzeria [σprice>price2( Explanation
on the next
πpizzeria,price(σpizza='pepperoni'Serves) page
×
ρpizzeria2,price2πpizzeria,price(σpizza='pepperoni'Serves))]
19. Find the pizzeria serving the cheapest pepperoni pizza. In the case of ties,
return all of the cheapest-pepperoni pizzerias.
1. Finds all pizzerias where price for pepperoni pizza is greater than in
some other pizzeria
σprice>price2(
πpizzeria,price(σpizza='pepperoni'Serves)
×
ρpizzeria2,price2[πpizzeria,price(σpizza='pepperoni'Serves)]
)
2. Subtracts it from all other pizzerias serving pepperoni pizzas
πpizzeria(σpizza='pepperoni'Serves)
−
π pizzeria [σprice>price2(
πpizzeria,price(σpizza='pepperoni'Serves)
×
ρpizzeria2,price2πpizzeria,price(σpizza='pepperoni'Serves))]