Open In App

PI() function in MySQL

Improve
Improve
Like Article
Like
Save
Share
Report

PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally.

Syntax :

PI()

Parameter :
This method does not accept any parameter.

Returns :
It returns the Pi value i.e. 3.141593.

Example-1 :
Returning the default value of Pi using PI Function.

SELECT PI() AS DefaultPiValue;

Output :

DefaultPiValue 
  3.141593

Example-2 :

Returning the value of Pi up to 18 decimal places using PI Function .  

SELECT PI()+0.000000000000000000 
AS PiValue;

Output :

PiValue 
3.141592653589793000

Example-3 :
Using PI Function to calculate the area and perimeter of all circles in a column. To demonstrate, let us create a table named Circle.

CREATE TABLE Circle(
Circle_id INT AUTO_INCREMENT,  
Radius DECIMAL(10, 3) NOT NULL,
PRIMARY KEY(Circle_id )
);

Now, insert some data to the Circle table.

INSERT INTO Circle(Radius )
VALUES
(2 ),(3),(10 ),(12.5 ),(6.80),
(4.60 ),(6),(20),(25) ;

So, the Circle Table is as follows.

SELECT * FROM Circle;
Circle_id Radius
1 2.000
2 3.000
3 10.000
4 12.500
5 6.800
6 4.600
7 6.000
8 20.000
9 25.000

Now, we will calculate the area and perimeter of every circle using PI function.

SELECT Circle_id, Radius,
PI() * Radius * Radius  AS Area,
2 * PI() * Radius AS Perimeter
FROM Circle;

Output :

Circle_id Radius Area Perimeter
1 2.000 12.566371 12.566371
2 3.000 28.274334 18.849556
3 10.000 314.159265 62.831853
4 12.500 490.873852 78.539816
5 6.800 145.267244 42.725660
6 4.600 66.476101 28.902652
7 6.000 113.097336 37.699112
8 20.000 1256.637061 125.663706
9 25.000 1963.495408 157.079633

Last Updated : 05 Oct, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads