0% found this document useful (0 votes)
11 views7 pages

Ucl Tuts

Uploaded by

Paul Benjamin
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)
11 views7 pages

Ucl Tuts

Uploaded by

Paul Benjamin
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/ 7

Pizza database exercises

1. Define each of the following SQL terms:


a. Base table: A database table that contains data.
b. Data definition language: SQL statements used to define database objects,
including creating, altering, and dropping tables, views, constraints, etc. For
example, DROP TABLE EMP.
c. Data manipulation language: SQL statements used to maintain and query a
database, including update, insert, and select statements.
d. Schema: A dictionary structure that contains descriptions of objects created
by a user, such as base tables, views, constraints, etc.
2. Consider the following Pizza database that consists of 3 tables:
MENU(pizza, price, country, base)
ITEMS(ingredient, type)
RECIPE(pizza, ingredient, amount)

3. Give CREATE TABLE statements to create the PIZZA database.

create table menu


( pizza varchar(20),
price real,
country varchar(20),
base varchar(20),
PRIMARY KEY (pizza) );

create table items


( ingredient varchar(12),
type varchar(8),
PRIMARY KEY (ingredient) );

create table recipe


( pizza varchar(20),
ingredient varchar(12),
amount integer,
PRIMARY KEY (pizza,ingredient),
FOREIGN KEY (pizza) REFERENCES menu,
FOREIGN KEY (ingredient) REFERENCES items );

▪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 distinct price


from menu
order by price;

6. List all Italian pizzas that cost more than $5.00.

select pizza
from menu
where price > 5 and country = 'italy';

7. List all pizzas except those that originate from Mexico.

select pizza
from menu
where not (country = 'mexico');

8. List pizzas that cost more than 6 and less than 7 dollars.

select pizza, price


from menu
where price > 6 and price < 7;

9. List Italian pizzas made with wholemeal base.

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;

b) select ingredient, type


from recipe natural join items
where pizza = 'stagiony';

3. List vegetable ingredients used in pizzas.


select pizza, ingredient, type
from items natural join recipe
where type = 'veg'
order by pizza;

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;

Note: ingredients are listed even if not used by any pizza.

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.

select distinct ingredient


from recipe
where pizza in (select pizza
from menu
where country = 'mexico')
order by ingredient;

▪4▪
11. Give pizzas and prices that are more expensive than all Italian pizzas (Hint: use > all
predicate).

select pizza, price


from menu
where price > all (select price from menu where country = 'italy');

12. List vegetarian pizzas (i.e. with no meat ingredients).

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.

select pizza, price


from menu
where price = (select min(price) from menu)

14. List pizzas costing more than the average price.

select pizza, price


from menu
where price > (select avg(price) from menu);

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).

select pizza, country, base


from menu
where pizza in (select pizza
from recipe
where ingredient = 'cheese')

18. Give the type of ingredients used for the least expensive pizza on the menu (use
subquery operation).

select distinct type


from items
where ingredient in (select ingredient
from recipe
where pizza =
(select pizza
from menu
where price = (select min(price) from menu)));
19. Give ingredients used in either the Margarita or the Vegetarian pizza (use the union
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.

create view fish


as select *
from menu
where pizza in (select pizza
from recipe r, items i
where r.ingredient = i.ingredient
and type = 'fish')

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▪

You might also like