Obtain Pi with Two Decimals

MySQLMySQLBeginner
Practice Now

Introduction

In this project, you will learn how to obtain the value of Pi rounded to two decimal places using an SQL statement in the MySQL client.

👀 Preview

MariaDB [(none)]> SOURCE /home/labex/project/getPi.sql;
+------+
| Pi   |
+------+
| 3.14 |
+------+
1 row in set (0.000 sec)

ðŸŽŊ Tasks

In this project, you will learn:

  • How to start the MySQL server using the sudo command
  • How to create an SQL script to display the value of Pi rounded to two decimal places
  • How to execute the SQL script in the MySQL client

🏆 Achievements

After completing this project, you will be able to:

  • Understand how to use the PI() function in MySQL to retrieve the value of Pi
  • Use the ROUND() function to round the value of Pi to a specific number of decimal places
  • Create and execute SQL scripts in the MySQL client

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`"]) mysql/BasicKeywordsandStatementsGroup -.-> mysql/source("`External Code Execution`") sql/BasicSQLCommandsGroup -.-> sql/select("`SELECT statements`") sql/DataManipulationandQueryingGroup -.-> sql/in("`IN clause`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/select("`Data Retrieval`") sql/AdvancedDataOperationsGroup -.-> sql/numeric_functions("`Numeric functions`") subgraph Lab Skills mysql/source -.-> lab-301368{{"`Obtain Pi with Two Decimals`"}} sql/select -.-> lab-301368{{"`Obtain Pi with Two Decimals`"}} sql/in -.-> lab-301368{{"`Obtain Pi with Two Decimals`"}} mysql/select -.-> lab-301368{{"`Obtain Pi with Two Decimals`"}} sql/numeric_functions -.-> lab-301368{{"`Obtain Pi with Two Decimals`"}} end

Start MySQL and Create the SQL Script

In this step, you will learn how to start the MySQL server and create the SQL script to display the value of Pi rounded to two decimal places.

  1. Open a terminal and start the MySQL server using the sudo command, which will not require a password:
sudo service mysql start
  1. Open the MySQL client by running the following command:
sudo mysql
  1. Open the getPi.sql file, add the following SQL query:
SELECT ROUND(PI(), 2) AS Pi;

This query uses the ROUND() function to round the value of PI() to two decimal places and assigns the result to the alias Pi.

Execute the SQL Script

In this step, you will learn how to execute the SQL script you created in the previous step.

  1. In the MySQL client, execute the SQL script by running the following command:
SOURCE /home/labex/project/getPi.sql;

This command will execute the SQL script and display the value of Pi rounded to two decimal places.

You should see the following output:

+------+
| Pi   |
+------+
| 3.14 |
+------+
1 row in set (0.000 sec)

The output shows the value of Pi rounded to two decimal places, as specified in the SQL query.

âœĻ 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