Databases - Lab Sheet 1: Gordon Royle
Databases - Lab Sheet 1: Gordon Royle
Gordon Royle
A NSWER : 4
A NSWER : 3
A NSWER : 303
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)
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)
A NSWER : ABW
A NSWER : ZWE
Q UESTION :
A NSWER : Aruba
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)
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
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)
A NSWER : 404424.12644059607
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
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