How to generate Fibonacci Series in SQL Server

How to generate Fibonacci Series in SQL Server? This is a very commonly asked interview question for SQL developers. I assume you must be knowing what is Fibonacci Series? In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterised by the fact that every number in it is the sum of the two preceding ones.

Example: 0,1,1,2,3,5,8,13,21,34,55 …

Let’s get to the solution.

1. Using spt_values:

Other T-SQL tricks using spt_values

2. Using CTE:

 

Prasad Sahoo

Prasad Sahoo, is an enthusiast of Microsoft technologies in general and a passionate database professional, mainly focusing on SQL Server performance tuning and business intelligence. He has contributed a long time of his career working on SQL Server and other RDBMS. If he is not busy with SQL stuff then he must be watching some Hollywood movies.

One thought on “How to generate Fibonacci Series in SQL Server

Leave a Reply

%d bloggers like this: