Summary: in this tutorial, you will learn how to use the SQLite pi() function to return the Pi value.
Introduction to SQLite pi() function #
In math, Pi is a constant that represents the ratio of a circle’s circumference to its diameter. It is denoted by the Greek letter π
Pi approximately equals 3.14159 and its decimal representation can go on infinitely without repeating.
SQLite pi() function returns the constant value of Pi.
The following shows the syntax of the pi() function:
pi()
The pi() function takes no argument and returns the pi value with an accuracy of 15 digits.
SQLite pi() function examples #
Let’s explore some examples of using the SQLite pi() function.
1) Basic pi() function example #
The following example uses the function to return the Pi constant:pi()
SELECT pi() pi;
Output:
pi
-----------------
3.141592653589793
2) Using the pi() function to calculate circumferences of circles #
First, create a table called circles:
CREATE TABLE circles(
id INTEGER PRIMARY KEY,
radius REAL
);
Second, insert rows into the circles table:
INSERT INTO
circles (radius)
VALUES
(1),
(2),
(3.5),
(5),
(NULL);
Third, query data from the circles table:
SELECT * FROM circles;
Output:
id | radius
---+-------
1 | 1.0
2 | 2.0
3 | 3.5
4 | 5.0
5 | NULL
(5 rows)
Finally, calculate the area of circles using the pi() function:
SELECT
pi() * radius * radius area
FROM
circles;
Output:
area
------------------
3.141592653589793
12.566370614359172
38.48451000647496
78.53981633974483
NULL
(5 rows)
The following uses the round() function to round the areas to numbers with two decimal precisions:
SELECT
round(pi() * radius* radius, 2) area
FROM
circles;
Output:
area
-------
3.14
12.57
38.48
78.54
NULL
(5 rows)
Summary #
- Use the
pi()function to return the Pi value, which is approximately equal to 3.141592653589793.
Thank you for your feedback!