SQL Quiz A – 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. Display all the baskets, ingredients and their cost prices. Display
with the highest cost price first.
Select basket, ingredient, costprice
From Baskets, Ingredients
Order by 3 desc
Q2. Find the total cost price of a ‘Fruitopia’ basket.
Select basket, sum(costprice * howmany) as total_costprice
From Baskets, Ingredients
Where basket = ‘Fruitopia’;
Group by basket;
Q3. Which basket has the highest sale price, and what is this price ?
Select basket, saleprice
From baskets
Where saleprice >= (select max(saleprice) from baskets);
Q4. What is the average saleprice of a basket ?
Select avg(saleprice) as averageprice
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.
Update baskets
Set saleprice = saleprice + 1
300418 1|Page
SQL Quiz A – Sample
Database Fundamentals
Where saleprice < (sekect avg(saleprice) from baskets);
300418 2|Page
SQL Quiz A – Sample
Database Fundamentals
For Questions 6 to 9, use the tables
girls ( girl, age, suburb )
e.g. Kate is 17 and lives at Burwood.
likessport ( girl, sport)
e.g. Kim likes tennis ( and other sports )
likesreading ( girl, magazine )
e.g. Joyce likes reading Who ( and other magazines ).
Q6. Find the number of girls who live in each suburb. Display with the
largest number first..
Select count(girl), suburb
From girls
Group by suburb
Order by 1 desc
Q7. Display the girls who like both tennis and reading Time magazine.
Select [Link]
From likessport ls, likesreading lr
Where sport = “tennis”
And magazine = “Time”;
Q8. Find the girl, and the number of sports liked, by the girl who likes
the largest number of sports.
Select girl, count(sport) as numberofsports
From likessport
Group by girl
Having count(sport) >= all (select count(sport)
From likessport
Group by girl)
Order by 1;
Q9. Girls aged less than 19 are not allowed to like tennis. Change the
tables.
300418 3|Page
SQL Quiz A – Sample
Database Fundamentals
Delete from llikessport
Where sport = “tennis”
And girl in (select girl from girls where age < 19);
300418 4|Page