MySQL - PI() Function



The MySQL PI() function returns the π (pi) value. The value 'Pi' is mathematically defined as the ratio of a circle's circumference to its diameter. It is a constant value, and equal to 3.141592653589793... It is an irrational number since it is non-terminating and non-repeating decimal.

This function does not accept any arguments and returns the approximated value of 'Pi' up to 6 decimal places, i.e. 3.141593. You can also perform various numerical operations using this function for mathematical calculations on the data.

Syntax

Following is the syntax of this function −

PI();

Parameters

This function does not accept any parameters.

Return Value

This function returns the mathematical constant π (pi) value.

Example

The following query uses MySQL PI() function to retrieve the mathematical constant π (pi) −

SELECT PI() As Result;

Output

This will produce the following result −

Result
3.141593

Example

Following is another example of this function, where we are calculating the ceiling value (smallest integer greater than or equal to) of the mathematical constant π

SELECT CEILING(PI()) As Result;

Output

Following is the output −

Result
4

Example

By default, this function prints up to 7 decimal places you can print more digits as shown below −

SELECT PI()+0.000000000000000 As Result;

Output

The output is displayed as follows −

Result
3.141592653589793

Example

In the following query, we are creating a MySQL table named SHAPES using the CREATE statement −

CREATE TABLE SHAPES (
   ID int,
   NAME varchar(20),
   COLOUR varchar(20),
   RADIUS int,
   PRIMARY KEY(ID)
);

The following query inserts 5 records into above created table −

INSERT INTO SHAPES (ID,NAME,COLOUR,RADIUS) VALUES 
(1, 'CIRCLE_b', 'BLUE', 10),
(2, 'CIRCLE_y', 'YELLOW', 20),
(3, 'CIRCLE_g', 'GREEN', 30),
(4, 'CIRCLE_w', 'WHITE', 40),
(5, 'CIRCLE_o', 'ORANGE', 50);	

Execute the following query to display all the records present in the SHAPES table −

Select * From SHAPES;

Following is the SHAPES table −

ID NAME COLOUR RADIUS
1 CIRCLE_b BLUE 10
2 CIRCLE_y YELLOW 20
3 CIRCLE_g GREEN 30
4 CIRCLE_w WHITE 40
5 CIRCLE_o ORANGE 50

Now, we are using the PI() function to calculate the area of circles −

SELECT *, PI()*radius*radius as AREA FROM SHAPES;

The output for the query above is produced as given below −

ID NAME COLOUR RADIUS AREA
1 CIRCLE_b BLUE 10 314.159265
2 CIRCLE_y YELLOW 20 1256.637061
3 CIRCLE_g GREEN 30 2827.433388
4 CIRCLE_w WHITE 40 5026.548246
5 CIRCLE_o ORANGE 50 7853.981634
Advertisements