Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:21:41+00:00 2026-06-03T00:21:41+00:00

I have a table Person that has 3 columns: Id, Name, ParentId where ParentId

  • 0

I have a table Person that has 3 columns: Id, Name, ParentId where ParentId is the Id of the parent row.

Currently, to display the entire tree, it would have to loop through all child elements until there’s no more child elements. It doesn’t seem too efficient.

Is there a better and more efficient way to query this data?

Also, is there a better way to represent this tree like structure in a SQL Server database? An alternative design for my table/database?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:21:43+00:00Added an answer on June 3, 2026 at 12:21 am

    I don’t think there’s anything wrong with the design, assuming you have a limited level of parent-child relationships. Here is a quick example of retrieving the relationship using a recursive CTE:

    USE tempdb;
    GO
    
    CREATE TABLE dbo.tree
    (
        ID INT PRIMARY KEY,
        name VARCHAR(32),
        ParentID INT FOREIGN KEY REFERENCES dbo.tree(ID)
    );
    
    INSERT dbo.tree SELECT 1, 'grandpa', NULL
    UNION ALL SELECT 2, 'dad', 1
    UNION ALL SELECT 3, 'me', 2
    UNION ALL SELECT 4, 'mom', 1
    UNION ALL SELECT 5, 'grandma', NULL;
    
    ;WITH x AS
    (
        -- anchor:
        SELECT ID, name, ParentID, [level] = 0
        FROM dbo.tree WHERE ParentID IS NULL
        UNION ALL
        -- recursive:
        SELECT t.ID, t.name, t.ParentID, [level] = x.[level] + 1
        FROM x INNER JOIN dbo.tree AS t
        ON t.ParentID = x.ID
    )
    SELECT ID, name, ParentID, [level] FROM x
    ORDER BY [level]
    OPTION (MAXRECURSION 32);
    GO
    

    Don’t forget to clean up:

    DROP TABLE dbo.tree;
    

    This might be a useful article. An alternative is hierarchyid but I find it overly complex for most scenarios.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table that has the following columns: Team Region Person Name and
I have a database structure that has a Person table which contains fields such
I have a table that has (for example) 4 columns. pk_table_id INT NOT NULL
Let's say I have a table called Person with the columns: id, name, parent_id
I have a Person table and Phone table. The Phone table has a foreign
I have table with date/time column and person column. Each person has multiple records
I have three tables like this: Person table: person_id | name | dob --------------------------------
I inherited a mysql database that has a table with columns like this: object_id,
If I have three columns in my MySQL table people , say id, name,
I have a database that has four columns like this level_1, level_2, level_3, level_4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.