• Neeraj

MemberJul 2, 2013

## 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.
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
• MemberJul 2, 2013

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';
Are you sure? This action cannot be undone.
• MemberJul 2, 2013

Makes sense. How about attempting the others too?
I have added another question to the existing ones..
Are you sure? This action cannot be undone.
• MemberJul 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
Are you sure? This action cannot be undone.