Querying Official Languages and Sorting

MySQLMySQLBeginner
Practice Now

Introduction

In this project, you will learn how to query and sort the official languages spoken in different countries using SQL. You will access the MySQL database, import the necessary data, and write a SQL script to retrieve and order the official languages.

👀 Preview

MariaDB [world]> SOURCE /home/labex/project/getOfficialLanguageOrder.sql;
+-------------+------------+
| CountryCode | Language   |
+-------------+------------+
| ABW         | Dutch      |
| AFG         | Dari       |
| AFG         | Pashto     |
| AIA         | English    |
| ALB         | Albaniana  |
| AND         | Catalan    |
| ANT         | Dutch      |
| ANT         | Papiamento |
| ARE         | Arabic     |
| ARG         | Spanish    |
+-------------+------------+
10 rows in set (0.000 sec)

ðŸŽŊ Tasks

In this project, you will learn:

  • How to access the MySQL database using the sudo command without a password
  • How to import data from a SQL script into the MySQL database
  • How to write a SQL query to retrieve the official languages spoken in each country, sorted by their country codes
  • How to run a SQL script in the MySQL database

🏆 Achievements

After completing this project, you will be able to:

  • Understand how to work with the MySQL database using the command line
  • Write SQL queries to extract and sort data from a database
  • Apply your SQL knowledge to solve real-world data manipulation problems

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/DatabaseManagementandOptimizationGroup(["`Database Management and Optimization`"]) mysql/BasicKeywordsandStatementsGroup -.-> mysql/source("`External Code Execution`") sql/BasicSQLCommandsGroup -.-> sql/select("`SELECT statements`") sql/DataManipulationandQueryingGroup -.-> sql/where("`WHERE clause`") sql/DataManipulationandQueryingGroup -.-> sql/in("`IN clause`") sql/DataManipulationandQueryingGroup -.-> sql/order_by("`ORDER BY clause`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/select("`Data Retrieval`") sql/DatabaseManagementandOptimizationGroup -.-> sql/using_indexes("`Using Indexes`") subgraph Lab Skills mysql/source -.-> lab-301398{{"`Querying Official Languages and Sorting`"}} sql/select -.-> lab-301398{{"`Querying Official Languages and Sorting`"}} sql/where -.-> lab-301398{{"`Querying Official Languages and Sorting`"}} sql/in -.-> lab-301398{{"`Querying Official Languages and Sorting`"}} sql/order_by -.-> lab-301398{{"`Querying Official Languages and Sorting`"}} mysql/select -.-> lab-301398{{"`Querying Official Languages and Sorting`"}} sql/using_indexes -.-> lab-301398{{"`Querying Official Languages and Sorting`"}} end

Access MySQL and Import Data

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

  1. Start the MySQL service:
sudo service mysql start
  1. Access the MySQL database using the sudo command without a password:
sudo mysql
  1. Import the data from the /home/labex/project/world.sql script into MySQL:
MariaDB [(none)]> SOURCE /home/labex/project/world.sql;

Query and Sort Official Languages

In this step, you will learn how to query the official languages spoken in each country listed in the countrylanguage table, sort them by their respective country codes, and retrieve the top 10 records.

  1. Create the getOfficialLanguageOrder.sql file.
  2. In the getOfficialLanguageOrder.sql file, add the following SQL query:
SELECT `CountryCode`, `Language`
FROM countrylanguage
WHERE IsOfficial = 'T'
ORDER BY CountryCode
LIMIT 10;

This query will select the CountryCode and Language attributes from the countrylanguage table, where the IsOfficial column is 'T' (true), order the results by the CountryCode column, and limit the output to the top 10 records.

  1. Save the getOfficialLanguageOrder.sql file.

Run the Script

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

  1. In the MySQL prompt, run the following command to execute the script:
MariaDB [world]> SOURCE /home/labex/project/getOfficialLanguageOrder.sql;

This will execute the SQL query in the getOfficialLanguageOrder.sql script and display the results in the MySQL prompt.

The output should look similar to the following:

+-------------+------------+
| CountryCode | Language   |
+-------------+------------+
| ABW         | Dutch      |
| AFG         | Dari       |
| AFG         | Pashto     |
| AIA         | English    |
| ALB         | Albaniana  |
| AND         | Catalan    |
| ANT         | Dutch      |
| ANT         | Papiamento |
| ARE         | Arabic     |
| ARG         | Spanish    |
+-------------+------------+
10 rows in set (0.000 sec)

Congratulations! You have successfully completed the project of querying and sorting the official languages spoken in different countries.

âœĻ Check Solution and Practice

Summary

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

Other MySQL Tutorials you may like