MySQL Practice Exercises with Solutions – Country Database – Part 8
We have split this collection of exercises on MYSQL into seven parts, this is the first one, then you will find the second part, the third part, the fourth part, the fifth part, the sixth part, seventh part, eighth part, and finally the ninth part these exercises are based on simple, advanced and finally complex MYSQL queries. These questions are based on the following two tables, Cities table and States table.
Cities table
+------+---------------+------------+----------+------------+ | id | name | population | surface | city_state | +------+---------------+------------+----------+------------+ | 1 | New York | 463333 | 15.31667 | 01 | | 2 | Albany | 391234 | 16.99663 | 01 | | 3 | Buffalo | 402356 | 10.12345 | 01 | | 4 | San Bruno | 153233 | 9.22147 | 02 | | 5 | SAN-Francisco | 205689 | 11.99632 | 02 | | 6 | SAN-Diego | 269988 | 19.89451 | 02 | | 7 | Houston | 197009 | 18.00001 | 03 | | 8 | Chicago | 239878 | 22.01250 | 04 | +------+---------------+------------+----------+------------+
States table
+----+------------+------------+ | id | state_code | state_name | +----+------------+------------+ | 1 | 01 | New York | | 2 | 02 | California | | 3 | 03 | Texas | | 4 | 04 | Illinois | | 5 | 978 | Florida | | 6 | 971 | Indiana | +----+------------+------------+
Questions
1. Get the list of the 3 most populated cities.
2. Get the list of the 3 cities with the smallest surface.
3. Get the list of states whose department number starts with “97”.
4. Get the names of the 3 most populated cities, as well as the name of the associated state.
5. Get the list of the name of each State, associated with its code and the number of cities within these States, by sorting in order to get in priority the States which have the largest number of cities.
6. Get the list of the 3 largest States, in terms of surface area.
7. Count the number of cities whose names begin with “San”.
8. Get the list of cities whose surface is greater than the average surface.
9. Get the list of States with more than 1 million residents.
10. Replace the dashes with a blank space, for all cities beginning with “SAN-” (inside the column containing the upper case names).