Ucl Tuts
Ucl Tuts
▪1▪
4. List all pizzas in descending alphabetic order; show all columns.
select *
from menu
order by pizza desc;
5. List all price categories recorded in the MENU table, eliminating duplicates.
select pizza
from menu
where price > 5 and country = 'italy';
select pizza
from menu
where not (country = 'mexico');
8. List pizzas that cost more than 6 and less than 7 dollars.
select pizza
from menu
where country = 'italy' and base = 'wm';
10. How many Italian pizzas are recorded in the MENU table.
select count(*)
from menu
where country = 'italy';
▪2▪
1. Explain the following SQL operations:
a. Equi-join: A join for which the joining condition is based on equality between values in
the joining columns.
b. Non-equijoin: A join for which the joining condition is NOT based on equality between
values in the joining columns, e.g. using >, or between condition
c. Self-join. A join that joins a table with itself - often associated with unary relationships,
e.g. Manager of Employees.
d. Inner-join: the result table includes rows that satisfy the joining condition.
e. Outer join: The result table includes rows even if the joining condition fails; the column
values of the “missing rows” are set to null.
2. List all ingredients and their type used in the Stagiony pizza.
a) select items.ingredient, type
from recipe, items
where pizza = 'stagiony'
and items.ingredient = recipe.ingredient;
4. List all dairy ingredients and the pizzas that use them.
select items.ingredient, pizza
from items left outer join recipe
on items. ingredient = recipe. ingredient
where items.type = 'dairy'
order by items.ingredient;
5. Give all pizzas that originate from the same country as the Stagiony pizza.
select m1.pizza
from menu m1, menu m2
where m1.country = m2.country
and m2.pizza = 'stagiony'
and m1.pizza <> 'stagiony';
▪3▪
6. List all pizzas that cost more than the Seafood pizza; list pizza name and price.
select m1.pizza, m1.price
from menu m1, menu m2
where m1.price > m2.price
and m2.pizza = 'seafood';
7. For the Mexicano pizza give the base, and list all ingredients and their type.
select m.pizza, m.base, i.ingredient, i.type
from menu m, recipe r, items i
where m.pizza = r.pizza
and r.ingredient = i.ingredient
and m.pizza = 'mexicano';
8. Give pizzas with price that falls between the price of Margarita and Napolitana pizzas.
select m1.pizza, m1.price
from menu m1, menu m2, menu m3
where m1.price between m2.price and m3.price
and m2.pizza = 'margarita'
and m3.pizza = 'napolitana'
order by 2;
9. List all ingredients for the Italian pizzas (i.e. COUNTRY = 'italy').
select ingredient
from recipe
where pizza in (select pizza
from menu
where country = 'italy');
10. List all ingredients used in Mexican pizzas (i.e. COUNTRY = 'mexico'). Organize the
list alphabetically, and avoid duplicates in the result.
▪4▪
11. Give pizzas and prices that are more expensive than all Italian pizzas (Hint: use > all
predicate).
select pizza
from menu
where pizza not in (select pizza
from recipe r, items i
where r.ingredient = i.ingredient
and i.type = 'meat' )
13. Give the name and price of the least expensive pizza.
15. Give the pizza which contains the largest amount of the cheese ingredient.
select pizza
from recipe
where amount = (select max(amount)
from recipe
where ingredient = 'cheese')
and ingredient = 'cheese';
▪5▪
16. Find the pizza which uses the smallest number of ingredients.
select pizza
from recipe
having count(*) =
(select min(count(*))
from recipe
group by pizza)
group by pizza;
17. List pizzas, their country of origin and base for all pizzas containing cheese ingredients
(use subquery operation).
18. Give the type of ingredients used for the least expensive pizza on the menu (use
subquery operation).
select ingredient
from recipe
where pizza = 'margarita'
union
select ingredient
from recipe
where pizza = 'vegetarian'
▪6▪
20. List ingredients used in both the Ham and the Americano pizzas (use the intersect
operation).
select ingredient
from recipe
where pizza = 'ham'
intersect
select ingredient
from recipe
where pizza = 'americano'
21. Create a view FISH containing pizzas that contain fish ingredients.
22. Using the view FISH to list all pizzas with fish ingredients that cost more than $7.50.
select *
from fish
where price > 7.5
23. Create a view TOTALS giving the total amount of ingredients used in each pizza.
create view totals(pizza, amt)
as select pizza, sum(amount)
from recipe
group by pizza
24. Using the view TOTALS find the pizza that uses the largest amount of ingredients.
select pizza
from totals
where amt = (select max(amt) from totals)
▪7▪