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 7964963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:02:17+00:00 2026-06-04T06:02:17+00:00

I have a table with a structure like (id, parent_id) in Oracle11g. id parent_id

  • 0

I have a table with a structure like (id, parent_id) in Oracle11g.

id    parent_id
---------------
1     (null)
2     (null)
3     1
4     3
5     3
6     2
7     2

I’d like to query it to get all the lines that are hierarchically linked to each of these id, so the results should be :

root_id  id    parent_id
------------------------
1        3     1
1        4     3
1        5     3
2        6     2
2        7     2
3        4     3
3        5     3

I’ve been struggling with the connect by and start with for quite some time now, and all i can get is a fraction of the results i want with queries like :

select connect_by_root(id) root_id, id, parent_id from my-table
start with id=1
connect by prior id = parent_id

I’d like to not use any for loop to get my complete results.

Any Idea ?

Best regards,
Jérôme Lefrère

PS : edited after first answer, noticing me i had forgotten some of the results i want…

  • 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-04T06:02:18+00:00Added an answer on June 4, 2026 at 6:02 am

    The query you posted is missing the from clause and left an underscore out of connect_by_root, but I’ll assume those aren’t actually the source of your problem.

    The following query gives you the result you’re looking for:

    select * from (
       select connect_by_root(id) root_id, id, parent_id
       from test1
       start with parent_id is null
       connect by prior id = parent_id)
    where root_id <> id
    

    The central problem is that you were specifying a specific value to start from, rather that specifying a way to identify the root rows. Changing id = 1 to parent_id is null allows the entire contents of the table to be returned.

    I also added the outer query to filter the root rows out of the result set, which wasn’t mentioned in your question, but was shown in your desired result.

    SQL Fiddle Example


    Comment Response:

    In the version provided, you do get descendants of id = 3, but not in such a way that 3 is the root. This is because we’re starting at the absolute root. Resolving this is easy, just omit the start with clause:

    SELECT *
    FROM
      (SELECT connect_by_root(id) root_id,
              id,
              parent_id
       FROM test1
    CONNECT BY
       PRIOR id = parent_id)
    WHERE root_id <> id
    

    SQL Fiddle Example

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

Sidebar

Related Questions

I currently have a table structure that looks something like this(some details omitted): ColumnName
I have a table for product category that has a hierarchical structure. Each Category_ID
I have a table that stores a tree like structure of file names. There
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I have a table structure like this: <table> <tr> <td> <ul> <li class=check></li> </ul>
I have my Table structure like this :: ATT_Table : Fields - Act_ID, Assigned_To_ID,
If I have a table structure like this: Transaction [TransID, ...] Document [DocID, TransID,
I have this table structure and would like to map it using Fluent Hibernate
I have a mysql database with a table structure like below : Table Name
I have an activity table with a structure like this: id prd_id act_dt grp

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.