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

  • nareshkumar6539
    nareshkumar6539
    Nick_Sharma
    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.

    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
    Makes sense. How about attempting the others too?
    I have added another question to the existing ones..
  • Neeraj Sharma
    Neeraj Sharma
    <>

    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

You are reading an archived discussion.

Related Posts

Are these two similar in any nature? I heard they are not But they are having almost similar coding blocks I know that Embedded C is used predeterministically for Configuring...
While interviewing some candidates, I found them talking more about their certifications than interests. There was this guy whom I interviewed who started saying "I am Sun certified Java professional,...
I am creating a pictorial tutorial for dual-boot of Windows & linux, I however found a way to save the screenshots from live images to cloud storage, but I also...
Here, I will be using Windows 7 & Xubuntu 13.04 for pictorial explanation. Considering, Windows 7 is pre-installed; we'll further move for the manual installation process. Download any Linux ISO...
Back in 2004 Apple launched Airplay which allowed wireless streaming of data (Audio,Video,Pics,etc) between devices. However, it's proprietary stack thus you will need Airplay compatible devices like iPad, iPhone &...