Вы находитесь на странице: 1из 11

LDBM Answers

MyFavs Database

1. Find people who are using facebook


SELECT *
FROM MyFavs__APP
WHERE app='facebook'

2. List all movies for which reason for liking has the word “special effects”. The query output
should not include any duplicates. (Hint: reason for liking can be found in the attribute “why-
I-like-it)
SELECT `movie-name`
FROM MyFavs__MOVIE
WHERE `why-I-like-it` LIKE '%special effects%'

3. What is/are the least favorite pizza topping/s. Display pizza topping.
(Hint: Least favorite means fewest number of people like them)

SELECT DISTINCT(`fav-pizza-topping`)
FROM MyFavs__PERSON as A
GROUP BY A.`fav-pizza-topping`
HAVING count(*) <= ALL (SELECT count(*)
FROM MyFavs__PERSON As B
GROUP BY B.`fav-pizza-topping`)

4. Display the average hours per week, and average rating for each apps

SELECT app, AVG (`hours-per-week`), AVG(`rating`)


FROM MyFavs__APP
GROUP BY app
5. Display secret-identifier of all persons who use at least ALL the apps that person with
secret-identifier “aha” uses
SELECT DISTINCT (`secret-identifier`)
FROM MyFavs__APP X
WHERE NOT EXISTS (SELECT *
FROM MyFavs__APP Y
WHERE `secret-identifier` = 'aha'
AND NOT EXISTS(SELECT *
FROM MyFavs__APP
WHERE `secret-identifier` = X.`secret-
identifier`
AND app = Y.app ))
AND `secret-identifier` != 'aha'

6. Which action movie has been watched in theatre by at least 3 persons (Hint: genre =
‘action’)
SELECT `movie-name`
FROM MyFavs__MOVIE
WHERE genre = 'action' AND `theatre-or-dvd` = 'theatre'
GROUP BY `movie-name`
HAVING count(*) >= 3
Coralbleach Database

1. List the names of all reefs that have the word “Is” in their names
SELECT reefname
FROM coralbleach__reef
WHERE reefname LIKE '%Is%'

2. List the names of all corals that have a thermal threshold of > 22 degrees
SELECT coralname, thermalthreshold
FROM coralbleach__coral
WHERE thermalthreshold > 22

3. Find names of all reefs that do not have a sample of the coral with name "Sea Mat and
Button Polyps (Palythoa)". Use a sub-query in your answer.
SELECT DISTINCT reefname
FROM coralbleach__reef
WHERE reefname NOT IN (SELECT reefname
FROM coralbleach__coralsampling
WHERE coralcode IN (SELECT coralcode
FROM coralbleach__coral
WHERE coralname = 'Sea Mat and Button Polyps
(Palythoa)'))

4. For each coral that has at least 2 samples during 1/1/2007 and 1/1/2008, list the coral
name and the number of samples. Note: date format in the database is stored as YYYY-mm-
dd
SELECT coralname, COUNT(*) AS `Sample Number`
FROM coralbleach__coral AS a, coralbleach__coralsampling b
WHERE a.coralcode = b.coralcode AND dateofsampling BETWEEN ('2007-
01-01') AND ('2008-01-01')
GROUP BY a.coralcode
HAVING COUNT(*) >= 2
5. Find the coral that has the highest average bleach percent across all its samples.
SELECT coralcode, coralname
FROM coralbleach__coral
WHERE coralcode IN (SELECT coralcode
FROM coralbleach__coralsampling
GROUP BY coralcode
HAVING AVG(bleachpercent) >= ALL (SELECT AVG(bleachpercent)
FROM coralbleach__coralsampling
GROUP BY coralcode))

6. List all dates where the temperature reading for reef "Heron Is. (south)" was greater than
22 degrees
SELECT dateofreading
FROM coralbleach__reeftemp
WHERE temperaturereading > 22 AND reefname = 'Heron Is. (south)'

7. How many reefs have had a temperature reading > 22 degrees after 1/8/2008 (date format
in the database is 2008-08-1)
SELECT count(*) as 'Number of Reefs'
FROM coralbleach__reeftemp
WHERE temperaturereading > 22 AND dateofreading > '2008-08-1'

8. Find all corals that have a thermal threshold of > 21 and have any single bleach percent of
more than 10%
SELECT DISTINCT(a.coralname), a.coralcode, a.thermalthreshold
FROM coralbleach__coral a, coralbleach__coralsampling b
WHERE a.coralcode = b.coralcode AND a.thermalthreshold > 21 AND
b.bleachpercent > 10

9. List details of all samples taken from "Keppel-Halfway Is." Reef


SELECT a.*, b.coralname
FROM coralbleach__coralsampling a, coralbleach__coral b
WHERE a.coralcode = b.coralcode AND reefname = 'Keppel-Halfway Is.'
10. Find the names of reefs that have a sample of ALL corals that the reef "Wreck Is." has.
SELECT DISTINCT reefname
FROM coralbleach__coralsampling AS A
WHERE NOT EXISTS (SELECT *
FROM coralbleach__coralsampling
WHERE reefname = "Wreck Is."
AND coralcode NOT IN (SELECT coralcode
FROM coralbleach__coralsampling
WHERE reefname = A.reefname))
Olympics Database

1. Select the names of all athletes that won medals in the 2000 Olympics
SELECT name
FROM olympics__athlete
WHERE athleteID IN (SELECT athleteID
FROM olympics__results
WHERE year = 2000)

2. Select all the athletes that have competed at a summer Olympics


SELECT name
FROM olympics__athlete
WHERE athleteID IN (SELECT athleteID
FROM olympics__results
WHERE type = "Summer")

3. Select the athletes that won medals in the "Sydney Olympic Park Aquatic Centre". Try to
write this without the knowledge that the 2000 Olympics were in Sydney
SELECT *
FROM olympics__athlete
WHERE athleteID IN (SELECT athleteID
FROM olympics__results
WHERE (result = "Gold" OR result = "Silver"
OR result = "Bronze")
AND year IN (SELECT year
FROM olympics__eventLocation
WHERE venueID IN (SELECT venueID
FROM olympics__venue
WHERE name = "Sydney Olympic Park Aquatic Centre")))
4. Find a list of all the countries and their total medal counts over all Olympics
SELECT representativeCountry as Country, count(*) as MedalCount
FROM olympics__athlete A, olympics__results B
WHERE A.athleteID = B.athleteID
AND (result = "Gold" OR result = "Silver" OR result = "Bronze")
GROUP BY representativeCountry

5. Find the country with the most medals won without using the LIMIT function.
SELECT representativeCountry as Country, count(*) as MedalCount
FROM olympics__athlete A, olympics__results B
WHERE A.athleteID = B.athleteID
AND (result = "Gold" OR result = "Silver" OR result = "Bronze")
GROUP BY representativeCountry
HAVING count(*) >= ALL (SELECT count(*) as MedalCount
FROM olympics__athlete A, olympics__results B
WHERE A.athleteID = B.athleteID
AND (result = "Gold" OR result = "Silver"
OR result = "Bronze")
GROUP BY representativeCountry)

6. Find all the countries that competed at all the locations used during the 2000 Olympics
SELECT DISTINCT representativeCountry
FROM olympics__athlete A
WHERE NOT EXISTS (SELECT *
FROM olympics__eventLocation
WHERE year = 2000 AND eventID NOT IN (SELECT eventID
FROM olympics__results B
WHERE year=2000
AND (result="Gold" OR result="Silver"
OR result="Bronze")
AND B.athleteID IN (SELECT athleteID
FROM olympics__athlete C
WHERE C.representativeCountry =
A.representativeCountry)))
Property Database
1. List the names of all suburbs that have an average rate < 2.9
SELECT suburbname, avgrate
FROM property__SUBURB
WHERE avgrate < 2.9

2. List the unitcode and date_of_installation of meters that have a metertype which has a
description of "conventional"
SELECT unitcode,date_of_installation,A.description
FROM property__METERTYPE A, property__PROPERTYMETER B
WHERE A.metertype = B.metertype AND A.description = 'conventional'

3. List the pid and complete address (stno, stname, suburbname, postcode) of all properties
that have the letter "B"• OR "C" in the name of the customer and have number of occupants
> 3.
SELECT pid, P.stno, P.stname, S.suburbname, S.postcode
FROM property__PROPERTY P, property__CUSTOMER C, property__SUBURB S
WHERE S.suburbcode = C.suburbcode AND P.TFN = C.TFN
AND P.number_of_occupants > 3
AND (C.name LIKE '%B%' OR C.name LIKE '%C%')

4. List the pids of all properties that do not have the metertype “advanced” installed. Use a
sub-query in your answer.
SELECT pid
FROM property__PROPERTYMETER
WHERE pid NOT IN (SELECT pid
FROM property__PROPERTYMETER
WHERE metertype IN (SELECT metertype
FROM property__METERTYPE
WHERE description = 'advanced'))
5. Find the employees (empid and ename) who have never done a meter reading.
SELECT empid,ename
FROM property__METERREADER
WHERE empid NOT IN (SELECT empid
FROM property__METERREADING)

6. List the suburbs (suburbcode) that have less than 3 properties with a metertype
“advanced” installed.
SELECT suburbcode
FROM property__PROPERTY
WHERE pid NOT IN (SELECT pid
FROM property__PROPERTYMETER
WHERE metertype IN (SELECT metertype
FROM property__METERTYPE
WHERE description = 'advanced'))
GROUP BY suburbcode
HAVING COUNT(*) < 3

7. Find all properties that have same or more number_of_occupants as pid 2.


SELECT pid,number_of_occupants
FROM property__PROPERTY
WHERE number_of_occupants >= (SELECT number_of_occupants
FROM property__PROPERTY
WHERE pid = 2)
AND pid != 2

8. Find the customers whose home address (stno, stname, suburbcode) is different from any
property they own. Display all details of the customer.
SELECT *
FROM property__CUSTOMER C
WHERE stname NOT IN (SELECT stname
FROM property__PROPERTY P
WHERE C.TFN = P.TFN)
9. Find the suburb that that has the lowest average rate. Display suburbcode and the avgrate.
SELECT suburbcode, avgrate
FROM property__SUBURB
WHERE avgrate <= ALL (SELECT avgrate
FROM property__SUBURB)

10. Find the number of properties in suburbs that have average rate > 2.7
SELECT count(*) AS 'Number of Properties'
FROM property__PROPERTY
WHERE suburbcode IN (SELECT suburbcode
FROM property__SUBURB
WHERE avgrate > 2.7)

11. Find the number of readings done by each meter reader (empid) in 2009
SELECT empid, count(*) AS 'Number of Readings'
FROM property__METERREADING
WHERE YEAR(timestamp) = 2009
GROUP BY empid

12. Find all employees' id who have done a meter reading for the same property more than
twice
SELECT empid, count(*) AS 'Number of Reading'
FROM property__METERREADING
GROUP BY empid, pid
HAVING count(*) > 2

13. How many “apartments” are there in the suburb of “Barton”


SELECT count(*)
FROM property__PROPERTY P, property__SUBURB S
WHERE P.suburbcode = S.suburbcode
AND propertytype= 'apartment' AND suburbname = 'Barton'
14. Find the properties (pids) that have at least ALL the meter types as the property id 7 has.
SELECT DISTINCT(pid)
FROM property__PROPERTYMETER M
WHERE NOT EXISTS (SELECT *
FROM property__PROPERTYMETER
WHERE pid=7 AND metertype NOT IN (SELECT metertype
FROM property__PROPERTYMETER
WHERE pid = M.pid))
AND pid != 7

15. Find customers who have a property in at least ALL the suburbs that customer with TFN
“32792526” has.
SELECT DISTINCT TFN
FROM property__PROPERTY P
WHERE NOT EXISTS (SELECT *
FROM property__PROPERTY
WHERE TFN = '32792526'
AND suburbcode NOT IN (SELECT suburbcode
FROM property__PROPERTY
WHERE tfn = P.tfn))
AND TFN <> '32792526'

Вам также может понравиться