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:
Data | x |
---|---|
Data 1 | 1 |
Data 2 | 2 |
Data 3 | 3 |
Data 4 | 4 |
Data 5 | 5 |
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:
Data | x | FACTORIAL_Value |
---|---|---|
Data 1 | 1 | 1 |
Data 2 | 2 | 2 |
Data 3 | 3 | 6 |
Data 4 | 4 | 24 |
Data 5 | 5 | 120 |
❮ PostgreSQL Functions