Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
60 views

Databases - Lab Sheet 1: Gordon Royle

This document provides solutions to 15 questions about data in database tables related to cities and countries. Each question is accompanied by a SQL query and the results of running that query to arrive at the answer. The questions range from counting rows, determining attributes, averaging and summing populations, and selecting cities that meet certain criteria.

Uploaded by

Fred
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Databases - Lab Sheet 1: Gordon Royle

This document provides solutions to 15 questions about data in database tables related to cities and countries. Each question is accompanied by a SQL query and the results of running that query to arrive at the answer. The questions range from counting rows, determining attributes, averaging and summing populations, and selecting cities that meet certain criteria.

Uploaded by

Fred
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Databases - Lab Sheet 1

Gordon Royle

School of Mathematics & Statistics


University of Western Australia

Gordon Royle (UWA) Lab 1 1 / 17


Solutions

Each slide shows


A question from the lab sheet
A SQL query
The output from that query
The solution derived from that query

Usually there will be many other ways to do this.

Gordon Royle (UWA) Lab 1 2 / 17


Q1

How many rows in the City table?


SELECT *
FROM City;
+------+----------------+-------------+------------+
| ID | Name | CountryCode | Population |
+------+----------------+-------------+------------+
| 1 | Kabul | AFG | 1780000 |
| 2 | Qandahar | AFG | 237500 |
| 3 | Herat | AFG | 186800 |
| 4 | Mazar-e-Sharif | AFG | 127800 |
...
| 4077 | Jabaliya | PSE | 113901 |
| 4078 | Nablus | PSE | 100231 |
| 4079 | Rafah | PSE | 92020 |
+------+----------------+-------------+------------+
4079 rows in set (0.00 sec)

Expected answer: 4079

Gordon Royle (UWA) Lab 1 3 / 17


Q2

Q UESTION : How many attributes does a city have?


mysql> DESCRIBE City;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

A NSWER : 4

Gordon Royle (UWA) Lab 1 4 / 17


Q3

Q UESTION : How many tables are in the world database?

mysql> SHOW TABLES;


+-----------------+
| Tables_in_world |
+-----------------+
| City |
| Country |
| CountryLanguage |
+-----------------+
3 rows in set (0.00 sec)

A NSWER : 3

Gordon Royle (UWA) Lab 1 5 / 17


Q4

Q UESTION : How many cities have a population between 500000 and


1000000 (inclusive)?
SELECT * FROM City
WHERE population >= 500000
AND Population <= 1000000;
+------+--------------------------------+-------------+------------+
| ID | Name | CountryCode | Population |
+------+--------------------------------+-------------+------------+
| 5 | Amsterdam | NLD | 731200 |
| 6 | Rotterdam | NLD | 593321
...
| 3821 | Oklahoma City | USA | 506132 |
| 4069 | Bulawayo | ZWE | 621742 |
+------+--------------------------------+-------------+------------+
303 rows in set (0.00 sec)

A NSWER : 303

Gordon Royle (UWA) Lab 1 6 / 17


Q5

Q UESTION : What is the name of the most populous city in the table?
SELECT *
FROM City
ORDER BY Population DESC
LIMIT 5;
+------+-----------------+-------------+------------+
| ID | Name | CountryCode | Population |
+------+-----------------+-------------+------------+
| 1024 | Mumbai (Bombay) | IND | 10500000 |
| 2331 | Seoul | KOR | 9981619 |
| 206 | So Paulo | BRA | 9968485 |
| 1890 | Shanghai | CHN | 9696300 |
| 939 | Jakarta | IDN | 9604900 |
+------+-----------------+-------------+------------+
5 rows in set (0.00 sec)

A NSWER : Mumbai (Bombay)

Gordon Royle (UWA) Lab 1 7 / 17


Q6

Q UESTION : How many different country codes are used in the table?
SELECT DISTINCT CountryCode
FROM City;
+-------------+
| CountryCode |
+-------------+
| AFG |
| NLD |
...
| ZWE |
| PSE |
+-------------+
232 rows in set (0.00 sec)

A NSWER : 232
(There are 239 distinct country codes in the table Country)

Gordon Royle (UWA) Lab 1 8 / 17


Q7

Q UESTION : What is the country code that is earliest in the alphabet?


SELECT CountryCode
FROM CITY
ORDER BY CountryCode ASC
LIMIT 1;
+-------------+
| CountryCode |
+-------------+
| ABW |
+-------------+
1 row in set (0.00 sec)

A NSWER : ABW

Gordon Royle (UWA) Lab 1 9 / 17


Q8

Q UESTION : What is the country code that is latest in the alphabet?


SELECT CountryCode
FROM CITY
ORDER BY CountryCode DESC
LIMIT 1;
+-------------+
| CountryCode |
+-------------+
| ZWE |
+-------------+
1 row in set (0.00 sec)

A NSWER : ZWE

Gordon Royle (UWA) Lab 1 10 / 17


Q9

Q UESTION :

mysql> SELECT Name, Code FROM Country ORDER BY Code LIMIT 1;


+-------+------+
| Name | Code |
+-------+------+
| Aruba | ABW |
+-------+------+
1 row in set (0.00 sec)

A NSWER : Aruba

Gordon Royle (UWA) Lab 1 11 / 17


Q10

Q UESTION : What is the country code for the country containing the English
city London?
SELECT CountryCode
FROM City
WHERE Name=London;
+-------------+
| CountryCode |
+-------------+
| GBR |
| CAN |
+-------------+
2 rows in set (0.00 sec)

A NSWER : GBR (need to check separately which of CAN/GBR is English)

Gordon Royle (UWA) Lab 1 12 / 17


Q11

Q UESTION : What is the largest city in the country that contains the other city
called London?
SELECT Name
FROM City
WHERE CountryCode = CAN
ORDER BY Population DESC;
LIMIT 1;
+-----------+
| Name |
+-----------+
| Montral |
+-----------+
1 row in set (0.00 sec)

A NSWER : Montreal

Gordon Royle (UWA) Lab 1 13 / 17


Q12

Q UESTION : What is the average city population for the cities in Germany?
SELECT AVG(Population)
FROM City
WHERE CountryCode = DEU;
+-----------------+
| AVG(Population) |
+-----------------+
| 282209.4946 |
+-----------------+
1 row in set (0.00 sec)

A NSWER : 282209.4946
(Some implementations of MySQL give a different number of decimal places)

Gordon Royle (UWA) Lab 1 14 / 17


Q13

Q UESTION :What is the standard deviation of the populations of the German


cities?
SELECT STD(Population)
FROM City
WHERE CountryCode = DEU;
+--------------------+
| STD(Population) |
+--------------------+
| 404424.12644059607 |
+--------------------+
1 row in set (0.00 sec)

A NSWER : 404424.12644059607

Gordon Royle (UWA) Lab 1 15 / 17


Q14

Q UESTION : How many Chinese cities have populations of strictly more than
1000000?
SELECT *
FROM City
WHERE Countrycode = CHN
AND Population > 1000000
+------+---------------------+-------------+------------+
| ID | Name | CountryCode | Population |
+------+---------------------+-------------+------------+
| 1890 | Shanghai | CHN | 9696300 |
| 1891 | Peking | CHN | 7472000 |
...
| 1923 | Jilin | CHN | 1040000 |
| 1924 | Tangshan | CHN | 1040000 |
+------+---------------------+-------------+------------+
35 rows in set (0.00 sec)

A NSWER : 35

Gordon Royle (UWA) Lab 1 16 / 17


Q15

Q UESTION : What is the total population of all the listed Chinese cities?
SELECT Sum(Population)
FROM City
WHERE Countrycode = CHN;
+-----------------+
| SUM(Population) |
+-----------------+
| 175953614 |
+-----------------+
1 row in set (0.00 sec)

A NSWER : 175953614

Gordon Royle (UWA) Lab 1 17 / 17

You might also like