MySQL PI() Function
MySQL PI() Function: Using the Pi Constant in SQL Queries
The PI() function returns the mathematical constant pi (3.141593 by default display) as a double-precision floating-point number. It takes zero arguments and always evaluates to the same constant.
You will use PI() when computing circular dimensions (circumference, area, cylinder volume), converting between degrees and radians manually, or building spherical distance queries across geographic points.
Syntax
PI()
- Takes no arguments. Passing any value inside the parentheses causes a syntax error.
Practical Example
Suppose demo_products sells circular dining tables and round rugs listed by radius_cm. You can compute each item's circumference (2 * PI() * r) and surface area (PI() * r * r) directly in SQL.
SELECT
product_id,
product_name,
radius_cm,
ROUND(2 * PI() * radius_cm, 2) AS circumference_cm,
ROUND(PI() * POW(radius_cm, 2), 2) AS area_sq_cm
FROM demo_products
WHERE radius_cm IS NOT NULL;
| product_id | product_name | radius_cm | circumference_cm | area_sq_cm |
|---|---|---|---|---|
| 81 | Round Oak Table | 60.0 | 376.99 | 11309.73 |
| 82 | Wool Circle Rug | 90.0 | 565.49 | 25446.90 |
| 83 | Small Side Table | 25.0 | 157.08 | 1963.50 |
Notice that ROUND(..., 2) formats the double-precision product cleanly into two decimal places for product specification sheets.
Common Mistakes & NULL Behavior
- Passing an argument to PI(): Writing
PI(10)orPI(NULL)throwsERROR 1582 (42000): Incorrect parameter count in the call to native function 'PI'. Always leave the parentheses empty:PI(). - Confusing display decimals with internal precision: Running
SELECT PI();prints3.141593(6 decimal places by default in MySQL clients), yet MySQL stores3.141592653589793internally as aDOUBLEfor accurate math. - No NULL inputs possible: Because
PI()accepts zero arguments, it never receivesNULLdirectly. MultiplyingPI() * radius_cmonly returnsNULLifradius_cmitself isNULL.
Frequently Asked Questions
How do I display more than 6 decimal places of PI() in MySQL?
Add 0.000000000000000 or use FORMAT(PI(), 15) or CAST(PI() AS DECIMAL(17, 15)) to inspect full internal precision (3.141592653589793).
Does PI() differ across MySQL platforms?
Internally, PI() represents the standard IEEE 754 DOUBLE approximation of pi (M_PI), so results are consistent across modern 64-bit servers.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.