SQLite pi() Function

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()Code language: JavaScript (javascript)

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 pi() function to return the Pi constant:

SELECT pi() pi;Code language: SQL (Structured Query Language) (sql)

Output:

pi
-----------------
3.141592653589793Code language: JavaScript (javascript)

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
);Code language: SQL (Structured Query Language) (sql)

Second, insert rows into the circles table:

INSERT INTO
  circles (radius)
VALUES
  (1),
  (2),
  (3.5),
  (5),
  (NULL);Code language: SQL (Structured Query Language) (sql)

Third, query data from the circles table:

SELECT * FROM circles;Code language: SQL (Structured Query Language) (sql)

Output:

id | radius
---+-------
1  | 1.0
2  | 2.0
3  | 3.5
4  | 5.0
5  | NULL

(5 rows)Code language: JavaScript (javascript)

Finally, calculate the area of circles using the pi() function:

SELECT
  pi() * radius * radius area
FROM
  circles;Code language: SQL (Structured Query Language) (sql)

Output:

area
------------------
3.141592653589793
12.566370614359172
38.48451000647496
78.53981633974483
NULL

(5 rows)Code language: JavaScript (javascript)

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;Code language: SQL (Structured Query Language) (sql)

Output:

area
-------
3.14
12.57
38.48
78.54
NULL

(5 rows)Code language: JavaScript (javascript)

Summary

  • Use the pi() function to return the Pi value, which is approximately equal to 3.141592653589793.
Was this tutorial helpful ?