Countries With 1 Billion+ Population

MySQLMySQLBeginner
Practice Now

Introduction

In this project, you will learn how to access MySQL, import data, and query information about countries with a population exceeding 1 billion. You will gain hands-on experience in working with MySQL databases and writing SQL queries to retrieve specific data.

👀 Preview

preview

🎯 Tasks

In this project, you will learn:

  • How to access MySQL using the sudo command without a password
  • How to import data from a SQL file into a MySQL database
  • How to write a SQL query to retrieve data from the city table and filter for countries with a population exceeding 1 billion
  • How to sort the query results in ascending order by population

🏆 Achievements

After completing this project, you will be able to:

  • Manage MySQL databases, including importing data and running SQL queries
  • Understand how to filter and sort data in SQL based on specific criteria
  • Apply your SQL knowledge to solve real-world data analysis problems
  • Gain practical experience in working with large datasets and extracting meaningful insights

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL mysql(("`MySQL`")) -.-> mysql/BasicKeywordsandStatementsGroup(["`Basic Keywords and Statements`"]) sql(("`SQL`")) -.-> sql/BasicSQLCommandsGroup(["`Basic SQL Commands`"]) sql(("`SQL`")) -.-> sql/DataManipulationandQueryingGroup(["`Data Manipulation and Querying`"]) sql(("`SQL`")) -.-> sql/AdvancedDataOperationsGroup(["`Advanced Data Operations`"]) sql(("`SQL`")) -.-> sql/DatabaseManagementandOptimizationGroup(["`Database Management and Optimization`"]) mysql/BasicKeywordsandStatementsGroup -.-> mysql/source("`External Code Execution`") sql/BasicSQLCommandsGroup -.-> sql/select("`SELECT statements`") sql/DataManipulationandQueryingGroup -.-> sql/in("`IN clause`") sql/DataManipulationandQueryingGroup -.-> sql/order_by("`ORDER BY clause`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/select("`Data Retrieval`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/use_database("`Database Selection`") sql/DataManipulationandQueryingGroup -.-> sql/group_by("`GROUP BY clause`") sql/DataManipulationandQueryingGroup -.-> sql/having("`HAVING clause`") sql/AdvancedDataOperationsGroup -.-> sql/numeric_functions("`Numeric functions`") sql/DatabaseManagementandOptimizationGroup -.-> sql/using_indexes("`Using Indexes`") subgraph Lab Skills mysql/source -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} sql/select -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} sql/in -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} sql/order_by -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} mysql/select -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} mysql/use_database -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} sql/group_by -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} sql/having -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} sql/numeric_functions -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} sql/using_indexes -.-> lab-373489{{"`Countries With 1 Billion+ Population`"}} end

Access MySQL and Import the Data

In this step, you will learn how to access MySQL using the sudo command without any password, and import the data from /home/labex/project/world.sql into the world database.

  1. Start the MySQL service:
sudo service mysql start
  1. Access MySQL without a password:
sudo mysql
  1. Import the data from the world.sql file:
SOURCE /home/labex/project/world.sql;
  1. Switch to the world database:
USE world;

Now you have successfully imported the data and switched to the world database.

Query Countries With Population Exceeding 1 Billion

In this step, you will learn how to query information about countries with a population exceeding 1 billion from the city table and sort the results in ascending order of population.

  1. Add the following SQL query to the file getBigPopulation.sql.
SELECT CountryCode AS 'Country Code', SUM(Population) AS 'Population'
FROM city
GROUP BY CountryCode
HAVING SUM(Population) > 100000000
ORDER BY SUM(Population) ASC;

This query will:

  • Select the CountryCode and the sum of Population for each country code
  • Group the results by CountryCode
  • Filter the results to only include country codes with a total population exceeding 1 billion (100,000,000)
  • Order the results in ascending order by the total population
  1. Save and close the file.

Run the SQL Script

In this step, you will learn how to run the getBigPopulation.sql script in MySQL.

  1. In the MySQL prompt, run the script:
SOURCE /home/labex/project/getBigPopulation.sql;

This will execute the SQL script and display the results:

+--------------+--------------+
| Country Code | Population   |
+--------------+--------------+
| IND          |  123298526   |
| CHN          |  175953614   |
+--------------+--------------+
2 rows in set (0.009 sec)

The output shows the country code and total population for the countries with a population exceeding 1 billion, sorted in ascending order by population.

Congratulations! You have successfully completed the project to retrieve data on countries with a population exceeding 1 billion.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other MySQL Tutorials you may like