Fibonacci series recursive query in PostgreSQL

Efficiently Generating the Fibonacci sequence in PostgreSQL with a Recursive CTE

Table of contents

No heading

No headings in the article.

Hello Everyone today we are going to see how to generate the Fibonacci series in PostgreSQL.

We are going to use WITH clause which allows us to have temporary tables but they only exist during the execution of the query.

Before Starting the Actually Query we need to know that the Fibonacci number is the sum of the two preceding numbers.

Fibonacci Sereies : 0,1,1,2,3,5,8,13....

So first of all, we need to set a base statement that will generate a base table on which we are going to generate the series.

We are going to have three columns:

1) Current Fibonacci Number

2) Previous Fibonacci Number

3) Index of that Fibonacci Number

Base Result Set Query:

SELECT 0 AS curfibnumber, 1 AS prefibnumber, 1 AS ind

Now to use this base result set and generate the next Number we have to use a With the clause in it.

WITH RECURSIVE fibonacci AS(
    SELECT 0 AS curfibnumber, 1 AS prefibnumber, 1 AS ind
    UNION
    -- Number generation Query Will be here
)
SELECT * FROM fibonacci

What this Query does is that it generates the base resultSet which we can use for further Quering the data.

Now In this case I am assuming we want to find out the First 10 Numbers of the Fibonacci series.

As We know that the Fibonacci number is the sum of two preceding numbers. then we can generate the query easily since we already have curfibnumber and prefibnumber in the base result set itself. So Query Goes Like this.

-- Sum of two preceding numbers.
SELECT curfibnumber + prefibnumber AS curfibnumber

But we also need to pass curfibnumber as prefibnumber and index with increment which will track the index of the Fibonacci number and all this execution happens in table name which we have in WITH clause. So the query will be:

SELECT 
    curfibnumber + prefibnumber AS curfibnumber, 
    curfibnumber AS prefibnumber, 
    ind + 1 AS ind 
FROM fibonacci

Now We will insert this query into with clause :

WITH RECURSIVE fibonacci AS(
    SELECT 0 AS curfibnumber, 1 AS prefibnumber, 1 AS ind
    UNION
    -- Number generation Query Will be here
    SELECT 
        curfibnumber + prefibnumber AS curfibnumber, 
        curfibnumber AS prefibnumber, 
        ind + 1 AS ind 
    FROM fibonacci
)
SELECT * FROM fibonacci

Till now we have not set any limits and I assumed in this blog I will find Fibonacci numbers till 10th index. So to do that We need to use the WHERE clause.

WITH RECURSIVE fibonacci AS(
    SELECT 0 AS curfibnumber, 1 AS prefibnumber, 1 AS ind
    UNION
    -- Number generation Query Will be here
    SELECT 
        curfibnumber + prefibnumber AS curfibnumber, 
        curfibnumber AS prefibnumber, 
        ind + 1 AS ind 
    FROM fibonacci
    WHERE ind < 10
)
SELECT * FROM fibonacci

It will show all columns, if we just want Fibonacci numbers columns we can do that easily.

WITH RECURSIVE fibonacci AS(
    SELECT 0 AS curfibnumber, 1 AS prefibnumber, 1 AS ind
    UNION
    -- Number generation Query Will be here
    SELECT 
        curfibnumber + prefibnumber AS curfibnumber, 
        curfibnumber AS prefibnumber, 
        ind + 1 AS ind 
    FROM fibonacci
    WHERE ind < 10
)
SELECT curfibnumber as "Fibonacci Sereies" FROM fibonacci

And that's how you generate the Fibonacci series using recursive Query in PostgreSQL.