# Calculate pi with SQL

Coordinator DB provides `pi()` when a query needs the mathematical constant. Use it directly for calculations such as the area of a circle; there is no reason to regenerate a series in a production calculation.

```sql
SELECT pi() AS pi_value, pi() * 5 * 5 AS circle_area
FORMAT csv;
```

## A series as a learning exercise

A finite Leibniz series demonstrates a generated table, conditional expressions, floating arithmetic and aggregation together:

```sql
SELECT 4.0 * sum(
    CASE WHEN generate_series % 2 = 0 THEN 1.0 ELSE -1.0 END
    / (2 * generate_series + 1)
) AS approximation
FROM generate_series(0, 9999)
FORMAT csv;
```

The endpoints are inclusive, so this query evaluates 10,000 terms. The decimal literals make the intended approximate arithmetic explicit. The series converges slowly; more terms add work without turning the result into an exact decimal representation of pi. Compare the approximation with `pi()` rather than expecting identical final digits. Use a small range while learning or inspecting its execution plan.

See [mathematical functions](https://coordinatordb.com/docs/reference/functions/regular-functions/math-functions) and [`generate_series`](https://coordinatordb.com/docs/reference/functions/table-functions/generate-series).
