In this post, I am sharing one user define function to generate Fibonacci Series in PostgreSQL.
Today Morning, I have started to take the interview for PostgreSQL Database Developer.
Generally, I am asking complex SQL Query, but this time I have asked to implement few mathematical logics like: “Write a small function to generate Fibonacci Series”.
Fibonacci Series means a series of numbers in which each number is the sum of the two preceding numbers.
e.g 1, 1, 2, 3, 5, 8, 13…
I am sharing this solution here, Database Developer should prepare yourself because Interviewer like, me can ask this kind of question.
Fibonacci Series Function:
CREATE OR REPLACE FUNCTION fn_Fibonacci(FNum INTEGER)
RETURNS SETOF INTEGER
WITH RECURSIVE cte(Num1,Num2) AS
GREATEST(Num1,Num2),Num1 + Num2 AS FibonacciSeries
WHERE Num2 < FNum
SELECT Num1 FROM cte;
Execute Function to generate Fibonacci Series upto 50:
SELECT *FROM fn_Fibonacci(50);
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.