0% found this document useful (0 votes)
267 views16 pages

Pizza Queries Solutions

The document provides examples of relational algebra exercises on a pizza dataset. It defines four relations - Person, Frequents, Eats, and Serves and provides solutions to 19 different relational algebra queries on this dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
267 views16 pages

Pizza Queries Solutions

The document provides examples of relational algebra exercises on a pizza dataset. It defines four relations - Person, Frequents, Eats, and Serves and provides solutions to 19 different relational algebra queries on this dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Relational-algebra exercises on

Pizza dataset
Solutions
Setup
Person ( name, age, gender )
Frequents ( name, pizzeria )
Eats ( name, pizza )
Serves ( pizzeria, pizza, price )

• Eats contains information about what type of pizza each


person (customer) eats (likes)
• Name is always the customer’s name
• Frequents record information about pizzerias that each
customer frequents (visits)
• Serves contains information about all possible pizza types for
each pizzeria
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))]

You might also like