Database Asignment 3
Database Asignment 3
Homework Assignment
1
• When writing a query, write the query in a way that it would work over all possible database
instances and not just for the given example instance!
• Some questions are marked as bonus. You do not have to answer these questions to get full points for
the assignment. However, you can get bonus points for these questions!
ingredient
recipe foodItem ounces
Pasta and Meatballs Pasta 50
recipe Meatballs 10
Pasta and Meatballs
name inventor kitchen Pasta and Meatballs Tomato Sauce 5
Pasta and Meatballs Le cook Italian Pasta and Meatballs Onions 1
Cheese Soup The french French Cheese Soup Onions 4
Burger Cowboys American Cheese Soup Cheese 15
Cheese Soup Bread 20
Burger Bread 10
Burger Ground Beef 20
foodItem
item type calories stock
Pasta Wheat product 20 foodItem shop price
Meatballs Meat 40 Pasta Aldi 5
Tomato Sauce Sauce 5 Meatballs Aldi 10
Onions Vegetables 1 Tomato Sauce Aldi 3
Cheese Diary 30 Tomato Sauce Walmart 3
Bread Wheat product 25 Cheese Treasury Island 15
Ground Beef Meat 45
Hints:
• The attribute recipe of relation ingredient is a foreign key to relation recipe. The attribute foodItem of
relation ingredient is a foreign key to relation foodItem.
Solution
Solution
Write a relational algebra expression that returns the number of food item types for which the average
calories for all food items of this type is higher than 40.
Write a relational algebra expression that returns the total calories per recipe (assume that calories in the
foodItem table are given per ounce).
Write a relational algebra expression that returns the cheapest price for making the recipe “Burger”. Assume that
the price in the stock table is given per ounce. Also assume that there might be more than one entry for each food
item (e.g., bread and ground Beef) in the stock table, i.e., a food item may be sold by different shops.
Solution
Write a relational algebra expression that returns all recipes that can be cooked from ingredients bought
from a single shop.