PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL FACTORIAL() Function



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

Syntax

FACTORIAL(number)

Parameters

number Required. Specify the number. It must be a non-negative integer.

Return Value

Returns the factorial of a given integer number.

Example 1:

The example below shows the usage of FACTORIAL() function.

SELECT FACTORIAL(1);
Result: 1

SELECT FACTORIAL(2);
Result: 2

SELECT FACTORIAL(3);
Result: 6

SELECT FACTORIAL(5);
Result: 120

SELECT FACTORIAL(10);
Result: 3628800

SELECT FACTORIAL('10');
Result: 3628800

Example 2:

Consider a database table called Sample with the following records:

Datax
Data 11
Data 22
Data 33
Data 44
Data 55

The statement given below can be used to calculate the factorial of column x values.

SELECT *, FACTORIAL(x) AS FACTORIAL_Value FROM Sample;

This will produce the result as shown below:

DataxFACTORIAL_Value
Data 111
Data 222
Data 336
Data 4424
Data 55120

❮ PostgreSQL Functions