Pen down these SQL queries

Hey all. Please try and attempt the following SQL queries

Suppose you have a binary tree structure represented as in attachment 1.jpg

Suppose the details of the figure in attachment are stored in a table as follows:

Parent Child
------ -----
A B
B D
B E
A C
C F

Assume that the tree has n number of nodes so the rows in the table will be having a lot of rows

Write SQL for:

1) Finding the leaf nodes.
2) Finding the child nodes of B.
3) Finding the nth child of node A.
4) For a given node, print the complete path from the ultimate parent till that node.

Please let me know in case you need any clarifications with these questions.

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • nareshkumar6539

    nareshkumar6539

    @nareshkumar6539-BKuVbx Jul 2, 2013

    Nick_SharmaHey all. Please try and attempt the following SQL queries

    Suppose you have a binary tree structure represented as in attachment 1.jpg

    Suppose the details of the figure in attachment are stored in a table as follows:

    Parent Child
    ------ -----
    A B
    B D
    B E
    A C
    C F

    Assume that the tree has n number of nodes so the rows in the table will be having a lot of rows

    Write SQL for:

    1) Finding the leaf nodes.
    2) Finding the child nodes of B.
    3) Finding the nth child of node A.

    Please let me know in case you need any clarifications with these questions.

    1) Finding the leaf nodes.
    Leaf nodes means we don`t have childs.SO in that table entries in child column not in Parent column will give leaf nodes
    So for getting leaf nodes
    select child from Table1 where child not in (select Parent from Table1);
    this will give leaf nodes.
    2)Finding the child nodes of B.
    select child from Table1 where Parent like 'B';

  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Jul 2, 2013

    Makes sense. How about attempting the others too?
    I have added another question to the existing ones..

  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Jul 4, 2013

    <<PING>>

    Nobody up for this? It will be great if people participate and then I reveal the answers so that it becomes quite fruitful for everyone