MCQ

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    |
+----+------------+------------+
Download the file containing the SQL statements allowing the creation of these tables. (Click here)

 

 

Questions

1. Get the list of the 3 most populated cities.

SELECT * 
FROM `cities` 
ORDER BY `population` DESC 
LIMIT 3;


MySQL SELECTMySQL SELECTSELECT statement is used to get data from a MySQL database. You can use this command at the mysql> command prompt as well as in…Read More MySQL ORDER BYMySQL ORDER BYThe ORDER BY keyword sorts the result set in ascending or descending order. To sort records in descending order, use DESC keyword. To sort in…Read More MySQL LIMITMySQL LIMITLIMIT keyword is used to limit the number of rows returned in the result of a query. It can be used in conjunction with SELECT,…Read More

 

 
2. Get the list of the 3 cities with the smallest surface.

SELECT * 
FROM `cities` 
ORDER BY `surface` ASC 
LIMIT 3;


MySQL ORDER BYMySQL ORDER BYThe ORDER BY keyword sorts the result set in ascending or descending order. To sort records in descending order, use DESC keyword. To sort in…Read More MySQL LIMITMySQL LIMITLIMIT keyword is used to limit the number of rows returned in the result of a query. It can be used in conjunction with SELECT,…Read More

 

 
3. Get the list of states whose department number starts with “97”.

SELECT * 
FROM `states` 
WHERE `state_code` LIKE '97%';


MySQL LIKEMySQL LIKEThe LIKE operator is a logical operator used in WHERE clause that tests whether a string contains a specified pattern or not. Two wildcards are…Read More

 

 
4. Get the names of the 3 most populated cities, as well as the name of the associated state.

SELECT * 
FROM `cities` 
LEFT JOIN states ON state_code = city_state
ORDER BY `population` DESC 
LIMIT 3;


MySQL LEFT JOINMySQL LEFT JOINIn the SQL language, the LEFT JOIN command (also called LEFT OUTER JOIN) is a type of join between 2 tables. It allows listing all…Read More

 

 
 
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.

SELECT state_name, city_state, COUNT(*) AS nbr_items 
FROM `cities` 
LEFT JOIN states ON state_code = city_state
GROUP BY city_state
ORDER BY `nbr_items` DESC;


MySQL LEFT JOINMySQL LEFT JOINIn the SQL language, the LEFT JOIN command (also called LEFT OUTER JOIN) is a type of join between 2 tables. It allows listing all…Read More MySQL GROUP BYMySQL GROUP BYThe GROUP BY clause is used to group rows with the same values, and is used in SELECT statement with aggregation functions such as COUNT,…Read More

 

 
6. Get the list of the 3 largest States, in terms of surface area.

SELECT state_name, city_state, SUM(`surface`) AS state_surface 
FROM `cities` 
LEFT JOIN states ON state_code = city_state
GROUP BY city_state
ORDER BY state_surface DESC
LIMIT 3;


MySQL LEFT JOINMySQL LEFT JOINIn the SQL language, the LEFT JOIN command (also called LEFT OUTER JOIN) is a type of join between 2 tables. It allows listing all…Read More

 

 
7. Count the number of cities whose names begin with “San”.

SELECT COUNT(*) 
FROM `cities` 
WHERE `name` LIKE 'San%';


MySQL ORDER BYMySQL ORDER BYThe ORDER BY keyword sorts the result set in ascending or descending order. To sort records in descending order, use DESC keyword. To sort in…Read More MySQL LIKEMySQL LIKEThe LIKE operator is a logical operator used in WHERE clause that tests whether a string contains a specified pattern or not. Two wildcards are…Read More

 

 
8. Get the list of cities whose surface is greater than the average surface.

SELECT *
FROM `cities` 
WHERE `surface` > (SELECT AVG(`surface`) FROM `cities`);


SQL Wildcards in StatementSQL Wildcards in StatementA “wildcard” character is used to substitute for any other character in a string. These wildcards are used in the “LIKE” operator. Here are the…Read More

 

 
 
9. Get the list of States with more than 1 million residents.

SELECT city_state, SUM(`population`) AS city_population
FROM `cities`
GROUP BY `city_state`
HAVING city_population > 1000000
ORDER BY city_population DESC;


MySQL ORDER BYMySQL ORDER BYThe ORDER BY keyword sorts the result set in ascending or descending order. To sort records in descending order, use DESC keyword. To sort in…Read More MySQL HAVINGMySQL HAVINGHAVING clause is used in SELECT statement to specify filter conditions for a group of rows or aggregates. HAVING clause is often used with GROUP…Read More

 

 
10. Replace the dashes with a blank space, for all cities beginning with “SAN-” (inside the column containing the upper case names).

UPDATE `cities` 
SET name = REPLACE(name, '-', ' ') 
WHERE `name` LIKE 'SAN-%'


MySQL UPDATEMySQL UPDATEThe Update command is used to modify rows of a table. The Update command can be used to modify or update one or more fields…Read More

 

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *