> This page location: Math Functions > FACTORIAL
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL FACTORIAL() Function

**Info:** The factorial() function is a standard part of PostgreSQL, so everything here works with any Postgres deployment. If you're an enterprise building for the AI era, [Lakebase](https://www.databricks.com/product/lakebase) delivers managed Postgres with the performance, security, and native Lakehouse integration your teams need. If you're a developer or startup racing to ship and scale, [Neon](https://neon.com) gives you the fastest path to production Postgres.

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `factorial()` function to calculate the factorial of a number.

## Introduction to the PostgreSQL factorial() function

The factorial of a non-negative integer n is the product of all positive integers less than or equal to `n`:

```
n!=n×(n−1)×(n−2)×…×2×1
```

By convention, 0! = 1.

In PostgreSQL, you can use the built-in `factorial()` function to calculate the factorial of a number:

```sql
factorial(n)
```

In this syntax, `n` is the number that you want to calculate the factorial. The `factorial()` function returns null if n is null.

If n is negative, the factorial() function will issue an error:

```
ERROR:  factorial of a negative number is undefined
```

## PostgreSQL factorial() function example

Let's take some examples of using the `factorial()` function.

The following example uses the `factorial()` function to calculate the factorial of the number 10:

```sql
SELECT factorial(5);
```

Output:

```text
 factorial
-----------
       120
(1 row)
```

## Summary

- Use the `factorial()` function to calculate the factorial of a number.

---

## Related docs (Math Functions)

- [ABS](https://neon.com/postgresql/math-functions/abs)
- [CBRT](https://neon.com/postgresql/math-functions/cbrt)
- [CEIL](https://neon.com/postgresql/math-functions/ceil)
- [DEGREES](https://neon.com/postgresql/math-functions/degrees)
- [DIV](https://neon.com/postgresql/math-functions/div)
- [EXP](https://neon.com/postgresql/math-functions/exp)
- [FLOOR](https://neon.com/postgresql/math-functions/floor)
- [GCD](https://neon.com/postgresql/math-functions/gcd)
- [LCM](https://neon.com/postgresql/math-functions/lcm)
- [LN](https://neon.com/postgresql/math-functions/ln)
- [LOG](https://neon.com/postgresql/math-functions/log)
- [MOD](https://neon.com/postgresql/math-functions/mod)
- [MIN_SCALE](https://neon.com/postgresql/math-functions/min_scale)
- [PI](https://neon.com/postgresql/math-functions/pi-function)
- [POWER](https://neon.com/postgresql/math-functions/power)
- [RADIANS](https://neon.com/postgresql/math-functions/radians)
- [RANDOM](https://neon.com/postgresql/math-functions/random)
- [ROUND](https://neon.com/postgresql/math-functions/round)
- [SCALE](https://neon.com/postgresql/math-functions/scale)
- [SIGN](https://neon.com/postgresql/math-functions/sign)
- [SQRT](https://neon.com/postgresql/math-functions/sqrt)
- [TRIM_SCALE](https://neon.com/postgresql/math-functions/trim_scale)
- [TRUNC](https://neon.com/postgresql/math-functions/trunc)
- [WIDTH_BUCKET](https://neon.com/postgresql/math-functions/width_bucket)
