You are on page 1of 6

PQ1

1. List pizzas with the substring 'i' anywhere within the pizza name.

select pizza from menu where pizza like '%i%'

2. Give the average price of pizzas from each country of origin. Change the column name related to
average price to "average".

select country, avg(price) as average from menu where country is not null group by country

3. Give the average price of pizzas from each country of origin, do not list countries with only one pizza.

select country, avg(price) from menu where country is not null and country = 'italy' group by country

4. List all ingredients and their types for the 'margarita' pizza. Do not use a subquery.

select ingredient, type from items natural join recipe natural join menu where pizza = 'margarita'
5. Give pizzas and prices for pizzas that are more expensive than all Italian pizzas.

select pizza, price from menu where price >any(select max(price) from menu where country = 'italy')

6. Give all pizzas that originate from the same country as the 'siciliano' pizza. Do not include 'siciliano'
pizza in your result table.

select pizza from menu where country in (select country from menu where pizza = 'siciliano') and
pizza != 'siciliano'

7. List each ingredient and the pizza that contains the largest amount of this ingredient.

select recipe.ingredient, recipe.pizza, recipe.amount

from recipe

inner join

(select max(amount) amt, ingredient

from recipe

group by ingredient) rcp

on recipe.ingredient = rcp.ingredient and recipe.amount = rcp.amt

PQ2
1. List all pizzas, giving pizza name, price and country of origin where the country of origin has NOT been
recorded (i.e. is missing).
select pizza, price, country from menu where country is null-- Type query below

2. Give the most expensive pizza price from each country of origin. Sort your results by country in
ascending order.
select country, max(price) as most from menu where country is not null group by country

3. Give the average price of pizzas from each country of origin, only list countries with 'i' in the country's
name. Sort your results based on country in ascending order.

select country, avg(price) from menu where country like '%i%' and country is not null group by
country-- Type query below

4. List all 'fish' ingredients used in pizzas, also list the pizza names. Do not use a subquery.

select ingredient, pizza from items natural join recipe natural join menu where type = 'fish'

5. List all ingredients for the Mexican pizza (i.e. country = 'mexico'). You must use a subquery.
select distinct(ingredient) from recipe where pizza IN (select pizza from menu where country =
'mexico');
6. List all pizzas that cost more than 'stagiony' pizza, also give their prices.

select pizza, price from menu where price >any(select max(price) from menu where pizza =
'stagiony')

7. List ingredients used in more than one pizza. Sort your results in ascending order.

select ingredient from recipe group by ingredient, ingredient having count(pizza) > 1 order by 1

PQ3
1. List all price categories recorded in the MENU table, eliminating duplicates.

select distinct(price) from menu-- Type query below

2. Give the cheapest pizzas from each country of origin. Sort your results by country in ascending order.

select country, min(price) as least from menu where country is not null group by country order by 1
3. Give cheapest price of pizzas from each country of origin, only list countries with cheapest price of less
than $7.00

select country, min(price) as min from menu where country is not null group by country having
min(price) < 7 order by 1

4. List all 'meat' ingredients used in pizzas, also list the pizza names. Do not use a subquery.

select ingredient, pizza from items natural join recipe natural join menu where type = 'meat'

5. List pizzas with at least one 'meat' ingredient.You must use a subquery.

select distinct(pizza) from items natural join recipe natural join menu where type = 'meat'

6. List all pizzas that cost less than 'siciliano' pizza, also give their prices.

select pizza, price from menu where price <any(select max(price) from menu where pizza =
'siciliano')

7. List the ingredients, and for each ingredient, also list the pizza that contains the largest amount of this
ingredient.

select recipe.ingredient, recipe.pizza, recipe.amount

from recipe

inner join

(select max(amount) amt, ingredient

from recipe

SQL TEST

1. Which students have an ID number less than 5000?

Select student.id student.name from student where (((student.id)<50000))

2. What is the name of the faculty member whose ID is 4756?

Select faculty.id, faculty.name from faculty where faculty.id = 4756

3. What is the smallest section number used in the first semester of 2008

Select section.[no.], section.semester from [section] where section.[no.] <= 2712

4. How many students are enrolled in Section 2714 in the first semester of 2008?
Select registration.studentid, registration.sectionno from registration where registration.sectionno
= 2714

5. Which faculty members have qualified to teach a course since 1993? List the faculty ID,
course and date of qualifaction.

Select qualified.[faculty ID], qualified.courseid, qualified.datequalified from qualified where


qualified.datequalified >= “9/1993”

6. List all students who have a mark in subject ‘41116’ between 60 and 70. Show their student
number and mark and sort the data by marks in descending order.

SELECT sname

FROM student s, enroll e

WHERE e.sno=s.sno

AND e.subo='41116'

AND e.mark BETWEEN 60 AND 70

ORDER BY e mark DESC;

7. Give the subject number, and the number of students who have failed the subject (mark <
50) as ‘no_failing’. Sort the data in subject number order.

SELECT subno, COUNT(sno) AS "no_failing"

FROM enroll

WHERE mark< 50

GROUP BY subno

ORDER BY s bno ASC;

8. Show student number, and their average mark as ‘amark’ for each student. Only list students
who are enrolled in 4 subjects. List the results in student number order.

SELECT sno, AVG(mark) AS "amark"

FROM enroll

HAVING COUNT(subno) =4

GROUP BY sno
9. List student number, telephone number and mark for students enrolled in subject 30008
with a mark higher than 84. Sort the results by mark (descending) and then by student
number.

SELECT s.sno, s.telno, e.mark

FROM student s, enrol e

WHERE s.sno=e.sno

AND e.subno= '30008'

AND e.mark > 84

10. List Subject number and name for subjects which have an average mark that is higher than
75. List the results in descending subject number order.

SELECT sub.subno, sub.subname

FROM subject sub, enrol e

WHERE sub.subno= e.subno

HAVING AVG(e.mark) > 75


11. List subject number, name and quota for subjects with an average mark less than 60 and
more than 2 students enrolled. List the results in descending subject number order.

select subno,avg(mark) from enroll group by subno having count(sno)>2 order by subno desc;

12. Give the subject number, and the highest mark for each subject (as ‘highest_mark’). Only
consider the marks of students with the number ‘1’in their IDs. List the results in subject
number order.
select subno,max(mark) as 'Highest_mark'
from subject where sno like '%1%'
group by subno
order by subno;

You might also like