Sunteți pe pagina 1din 4

SQL Quiz B – Sample

Database Fundamentals

Write SQL statements

For Questions 1 to 5, use the tables

Baskets ( basket, saleprice, numberofbaskets )


e.g. Special basket sells for $20, and there are 35 in stock.

Ingredients ( ingredient, costprice)


e.g. banana costs $2 each

Contents ( basket, ingredient, howmany )


e.g. Special baskets have 5 bananas ( and other ingredients ).

Q1. Find the number of different ingredients in a ‘Special’ basket.


Select count(ingredient)
From contents
Where basket = “Special”;

Q2. Find the total cost price of a “Special’ basket.


Select sum(costprice*howmany), basket
From ingredients I , contents c
Where basket = “special”
And i.ingredient = c.ingredient;
Group by basket;

Q3. Which basket has the highest sale price, and what is this price ?
Select basket, saleprice
From baskets
Where saleprice >= all (select max(saleprice) from baskets)

Q4. What is the average saleprice of a basket ?


Select avg(saleprice)
From baskets

Q5. Change the data in the baskets table so that the sale price is
increased by $1 for all baskets which are below the average
saleprice.

300418 1|Page
SQL Quiz B – Sample
Database Fundamentals
Update baskets
Set saleprice = saleprice + 1
Where saleprice < (select avg(saleprice) from baskets);

300418 2|Page
SQL Quiz B – Sample
Database Fundamentals

For Questions 6 to 10, use the tables

likessport ( person, sport)


e.g. Kevin likes tennis ( and other sports )

likesreading( person, magazine )


e.g. Jimmy likes reading Who ( and other magazines ).

Q6. Find the number of people who like each magazine. Display with
the largest number first.
Select count(person), magazine
From likesreading
Group by magazine
Order by 1 desc, 2;

Q7. Display the people who like both football and reading Time
magazine.
Select ls.person
From likesport ls, likesreading lr
Where ls.person = lr.person
And sport = “football”
And magazine = “time”;

Q8. Find the person, and the number of sports liked, by the person
who likes the largest number of sports.
Select person, count(sport)
From likessport
Group by person
Having count(sport) >= all (select count(sport) from likessport group by
person);

Q9. People who read Time magazine are not allowed to like football.
Change the tables.
Delete from likessport
Where sport = “football”

300418 3|Page
SQL Quiz B – Sample
Database Fundamentals
And person in (select person from likesreading
Where magazine = “time”);

300418 4|Page

S-ar putea să vă placă și