Homework #5

advertisement
IS202 Database Systems (Spring 2006)
Lab. Assignment #2 (SOLUTIONS)
Write and run MySQL queries for the following questions. Also provide the query
results. If a query results in more than 10 tuples give only the first five and last five
rows (tuples)
1. Find those Africa cities with a population of more than 2 Million.
SELECT ci.name
FROM city ci, country co
WHERE ci.countrycode = co.code AND co.continent = ‘Africa’
AND ci.population > 2000000;
Output:
name
Alger
Luanda
Cairo
Alexandria
Giza
…
Addis Abeba
Nairobi
Kinshasa
Casablanca
Abidjan
11 rows fetched
2. Find the number of distinct languages in CountryLanguage table.
SELECT DISTINCT(Language)
FROM CountryLanguage cl;
Output:
language
Dutch
English
Papiamento
Spanish
Balochi
…
Xhosa
Bemba
Chewa
Lozi
Nsenga
457 rows fetched
3. Find those cities that are unique (the only city) in their districts.
SELECT name
FROM city c1
WHERE NOT EXISTS
(SELECT * FROM city c where district = c1.district AND name <> c1.name);
Output:
name
Kabul
Qandahar
Herat
Mazar-e-sharif
Almere
…
Khan Yunis
Hebron
Jabaliya
Nablus
Rafah
820 rows fetched
4. Find the languages spoken in Europe.
SELECT DISTINCT language
FROM Country co, CountryLanguage cl
WHERE co.continent = ‘Europe’ AND co.code = cl.code;
Output:
language
Arabic
Dutch
Fries
Turkish
Albaniana
…
Kazakh
Mari
Mordva
Tatar
Udmur
62 rows fetched
NOTE: If DISTINCT not used the query generates 202 rows
Output:
language
Arabic
Dutch
Fries
Turkish
Albaniana
…
Belorussian
Estonian
Finnish
Russian
Ukrainian
202 rows fetched
5. Find those Middle East countries with a LifeExpectancy longer than the average
LifeExpectancy of the European Countries.
SELECT name, LifeExpectancy
FROM Country
WHERE region = ‘Middle East’ AND
LifeExpectancy > (SELECT AVG(LifeExpectancy)
FROM Country
WHERE continent = ‘Europe’);
Output:
name
Israel
Jordan
Kuwait
Cyprus
4 rows fetched
6. Find those countries where official language is english and population is greater than
10 Million.
SELECT name, population
FROM Country c, CountryLanguage cl
WHERE c.countrycode = cl.code AND cl.language = 'English' AND IsOfficial = 'T'
AND c.population > 10000000;
Output:
name
Australia
United Kingdom
South Africa
Canada
United States
Zimbabwe
Download