Recursive SQL

One of the most common school exercice about recursion is the factorial. Guess what, I am going to do it in sql with hierarchies!

I use the following ln property :
x1*…*xn = exp(ln(x1)+..+ln(xn)))

Ok, here it is

SQL> select n, (select exp(sum(ln(level))) from dual connect by level<=n) "N!" from t1;

N N!
- ---
4 24
6 720

Published by

Laurent Schneider

Oracle Certified Master

Leave a Reply