Jump to content

Recommended Posts

I just finished a job interview/exam and I failed. :'(  I really don’t know how to implement this problem using an SQL.  You could check the exact question/problem below:

 

There are two tables, NAMES (ID INT IDENTITY (1,1), NAME VARCHAR(255) ) and RELATIONSHIPS (NAMEID INT, PARENT_NAMEID INT) linked via NAMES.ID = RELATIONSHIPS.NAMEID and where top-most name has a PARENT_NAMEID = 0.  Show a nested list of names including LEVEL, NAMEID and NAME, where LEVEL indicates the nest level (or depth) from the top, as indicated in the expected output (below).  You may use functions, stored procedures, views and any other Transact SQL commands compliant with Microsoft SQL 2000.

 

Sample Data

 

NAMES table content:

 

ID       NAME

1       Frank

2       Jo

3       Mary

4       Peter

5       Amy

 

RELATIONSHIPS table content:

 

NAMEID PARENT_NAMEID

1       0

2       1

3       2

4       1

5       2

 

Expected Output:

 

In the expected output, please note:

 

- The purpose –  rather than just value – of the LEVEL column

- The sequence of the output, in which child elements appear immediately beneath their respective parent elements.

 

LEVEL   ID   NAME

0       1       Frank

1       2       Jo

2       5       Amy

2       3       Mary

1       4       Peter

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.