PostgreSQL factorial() Function

The PostgreSQL factorial() function returns the factorial of a number.

factorial() Syntax

This is the syntax of the PostgreSQL factorial() function:

factorial(n) -> bigint

Parameters

n

Required. A positive integer or zero.

Return value

The PostgreSQL factorial() function returns the factorial of a number. For example, factorial(4) return the factorial of 4, that is 4 * 3 * 2 * 1 = 24.

The factorial() function will return NULL if the argument is NULL.

PostgreSQL will give an error if you supply a parameter that is not a numeric type.

factorial() Examples

Here are a few examples of the factorial() function:

SELECT
    factorial(0) AS "factorial(0)",
    factorial(5) AS "factorial(5)",
    factorial(10) AS "factorial(10)";
 factorial(0) | factorial(5) | factorial(10)
--------------+--------------+---------------
            1 |          120 |       3628800