Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 November PostgreSQL: Fibonacci Series Function for Database Developer Interview

PostgreSQL: Fibonacci Series Function for Database Developer Interview

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE OR REPLACE FUNCTION fn_Fibonacci(FNum INTEGER)
RETURNS SETOF INTEGER
LANGUAGE SQL
AS $dbrnd$
WITH RECURSIVE cte(Num1,Num2) AS
(
VALUES(0,1)
UNION ALL
SELECT
GREATEST(Num1,Num2),Num1 + Num2 AS FibonacciSeries
FROM cte
WHERE Num2 < FNum
)
SELECT Num1 FROM cte;
$dbrnd$;

Execute Function to generate Fibonacci Series upto 50:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT *FROM fn_Fibonacci(50);
 
fn_fibonacci
--------------
0
1
1
2
3
5
8
13
21
34
(10 rows)

Nov 15, 2016Anvesh Patel
PostgreSQL: Which Filter combinations and Operators can be used with Index?MySQL: Understand Case Sensitivity parameters for Effective Migration (Case Sensitive or Case Insensitive)
Comments: 2
  1. Smithc773
    June 22, 2018 at 1:57 am

    Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate?

    • Anvesh Patel
      Anvesh Patel
      June 25, 2018 at 7:16 pm

      You can contact me by Contact Me page.

Anvesh Patel
Anvesh Patel

Database Engineer

November 15, 2016 PostgreSQL, PostgreSQL InterviewAnvesh Patel, database, database research and development, dbrnd, Fibonacci Series Program, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....