/*
Pwc SQL Interview Question

Task:
Calculate the factorial of a given number using a recursive CTE
*/

-- Setting the number for which factorial is needed
SET @final_factorial = 6;

WITH RECURSIVE CTE AS(
 SELECT 1 AS number,1 AS factorial
 UNION
 SELECT 1 + number AS number, factorial * (1 + number) AS factorial
 FROM CTE
 WHERE number < @final_factorial
)

SELECT *
FROM CTE
ORDER BY number DESC
LIMIT 1;

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: