SQL Labsheet
SQL Labsheet
Required Software
1. mysql command line client. This is included in the mysql-essentials package or MySQL
Community Edition from www.mysql.com.
2. (Optional) Visual query tool for MySQL. The free MySQL Workbench (requires Visual C++
redistribution pack and .Net 4.0) or MySQL GUI Tools (does not require C++) are GUI tools for
accessing MySQL.
2. World database. You will use the database on se.cpe.ku.ac.th (nothing to install.) But,
if you want your own World database, see separate instructions for how to install.
Format of Exercises
Write the SQL command you use to get the data. If the result is requested, write that, too.
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------|
+-----------------+------------+
| Name | Population |
+-----------------+------------+
| Mumbai (Bombay) | 10500000 |
| Seoul | 9981619 |
| São Paulo | 9968485 |
+-----------------+------------+
+-----------+------------+
| Name | Population |
+-----------+------------+
| Adamstown | 42 |
+-----------+------------+
+-------------------+------------+-------------+
| Name | Population | CountryCode |
+-------------------+------------+-------------+
| Bangkok | 6320174 | THA |
| Nonthaburi | 292100 | THA |
| Nakhon Ratchasima | 181400 | THA |
| Chiang Mai | 171100 | THA |
| Udon Thani | 158100 | THA |
| Hat Yai | 148632 | THA |
| Khon Kaen | 126500 | THA |
| Pak Kret | 126055 | THA |
| Nakhon Sawan | 123800 | THA |
| Ubon Ratchathani | 116300 | THA |
| Songkhla | 94900 | THA |
| Nakhon Pathom | 94100 | THA |
+-------------------+------------+-------------+
SQL Database Lab Page 5
3.4 SELECT using WHERE, ORDER, and LIMIT
SELECT fields FROM table
WHERE condition
ORDER BY field [ASC|DESC]
LIMIT n
What Country has the largest population in Europe? What is its population?
sql> SELECT Name,Population FROM country WHERE Continent=’Europe’
ORDER BY Population DESC LIMIT 1;
Answer:
+--------------------+------------+
| Name | Population |
+--------------------+------------+
| Russian Federation | 146934000 |
+--------------------+------------+
+----------+
| Name |
+----------+
| Zambia |
| Zimbabwe |
+----------+
+----------+------------+
| Name | GNP*31 |
+----------+------------+
| Thailand | 3608896.00 |
+----------+------------+
4.4 What is the total GNP of the world (in millions USD)?
sql> SELECT SUM(GNP) FROM country;
Answer:
+-------------+
| SUM(GNP) |
+-------------+
| 29354907.90 |
+-------------+
SQL Database Lab Page 7
4.5 What are the richest countries in the world?
"Rich" means income per person, which is GNP/population. The GNP is in millions of US
Dollars, so multiply by 1,000,000 to get an answer in dollars.
What are the 2 richest countries in the world?
sql> SELECT Name, GNP*1000000/Population FROM country ORDER BY
GNP*1000000/Population DESC LIMIT 2;
Answer:
+-------------+------------------------+
| Name | GNP*1000000/Population |
+-------------+------------------------+
| Luxembourg | 37459.260959 |
| Switzerland | 36936.204681 |
+-------------+------------------------+
6 Modifying a Record
To change some values, use:
UPDATE tablename SET field1=value1, field2=value2 WHERE condition
Example: The "head of state" in the U.S.A. is now Barrack Obama.
UPDATE Country SET HeadOfState='Barrack Obama' WHERE code='USA';
6.2 What is the SQL to change the population of your City, using the City primary key?
sql> UPDATE city SET Population=900 WHERE ID=4081;
7 Deleting Data
DELETE FROM tablename WHERE condition
Example: delete all cities in Thailand named "Bangsaen":
DELETE FROM City WHERE name='Bangsaen' AND countrycode='THA';
7.1 What is the command to delete the City that you added to the database?
sql> DELETE FROM city WHERE Name=’Pattaya’ AND ID=4081;
9 JOIN Tables
A more semantic way of combining tables is to use JOIN
SELECT field1, field2, ...
FROM table1
JOIN table2
ON table1.field = table2.field
WHERE ... ORDER BY ...
You can assign an alias to table names to reduce typing. You can assign an alias to fields or
expressions, too.
Example: Print the capital city of the countries in Europe:
SELECT c.Name, co.Name AS CountryName, co.Region
FROM Country co
JOIN City c
ON co.Capital = c.ID
WHERE co.Continent = 'Europe'