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

  • SEARCH
  • Home
  • 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 8143673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:10:56+00:00 2026-06-06T13:10:56+00:00

I am writing a query which needs to return all child entries (and their

  • 0

I am writing a query which needs to return all child entries (and their children etc…) under any given parent ID.

For example:

SELECT id
FROM table
WHERE parent_id IN

    (SELECT id
    FROM table
    WHERE parent_id IN

        (SELECT id
        FROM table
        WHERE parent_id IN

            (SELECT id
            FROM table
            WHERE code = 'A01')
        )
    )

As expected, this will only return a result set for the topmost SELECT query. I could write a UNION SELECT for each child level but that seems a tad clumsy…

Is there a way to add each individual result set from each nested query into my main set of results? Or should I take the clumsy option?

  • 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-06T13:10:58+00:00Added an answer on June 6, 2026 at 1:10 pm

    Your problem is best solved with a recursive query. A great way to perform recursive queries in SQL Server 2005 or later is to use Common Table Expressions

    ;with cte as (
        SELECT id, parent_id
        FROM [table]
        WHERE code = 'A01'
    
        UNION ALL
    
        SELECT t.id, t.parent_id
        FROM [table] t
            INNER JOIN cte 
                ON cte.id = t.parent_id
    )
    SELECT id 
    FROM cte
    OPTION (MAXRECURSION 0);
    

    On a side note… I’m not sure I like the idea of the table name being table.

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

Sidebar

Related Questions

I am writing an SQL query which involves finding if timestamp falls in particular
I was writing a simple LINQ query which compares rows of a table with
I'm writing a select query in a C# method which lets client code provide
I'm writing a query to get ALL of the products in the products table,
I am writing an SQL query in which that I will need to perform
I am writing an iPhone application which in numerous places needs to perform non
I'm writing a query in SQL in which i have to find the youngest
I am having trouble writing query so that I can query the content of
When writing a query for paging on a web page what is the least
I'm writing this query and I'm running into a mental road block. I keep

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.