PostgreSQL Fibonacci

2014-04-04 1 min read

    Earlier today I was researching whether it was possible to generate Fibonacci numbers using a SQL query. A Google search turned up a short PostgreSQL query that uses a recursive approach. Since this is recursion, the query starts by defining a base case and then goes on to define a generation step with a stopping limit.

    with recursive f as (
        select 0 as a, 1 as b
        union all
        select b as a, a+b from f where a < 100000
    ) select a from f

    It’s interesting to see the edge features of a language and I find that query languages tend to have the most striking ones. My experience with the various SQLs has been that at the basic level they’re very similar but diverge significantly at the edges.