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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:11:44+00:00 2026-06-08T09:11:44+00:00

How do I find the entire tree given a node of a tree? Example

  • 0

How do I find the entire tree given a node of a tree?

Example of tree:

       100
  101        102
1010 1011   1020  1021


select level, employee_id, last_name, manager_id ,
       connect_by_root employee_id as root_id
  from employees
 connect by prior employee_id = manager_id
 start with employee_id = 101
;

The root in table is (parent,child) example (100,101) there is no (null,100) row in table.

The above query only gives the children starting from 101. But lets say I want everything from the start of the root?

When given ‘101’ as the node, you won’t know which is the root.

The query should be usable when the root is the given node.

  • 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-08T09:11:45+00:00Added an answer on June 8, 2026 at 9:11 am

    You need to first traverse up the tree to get all managers then traverse down to fetch all employees:

    select level, employee_id, last_name, manager_id ,
           connect_by_root employee_id as root_id
       from employees
    connect by prior employee_id = manager_id -- down the tree
    start with manager_id in ( -- list up the tree
         select manager_id 
           from employees
         connect by employee_id = prior manager_id -- up the tree
         start with employee_id = 101
         )
    ;
    

    See http://www.sqlfiddle.com/#!4/d15e7/18

    Edit:

    If the given node might also be the root node, extend the query to include the given node in the list of parent nodes:

    Example for non-root node:

    select distinct employee_id, last_name, manager_id 
       from employees
    connect by prior employee_id = manager_id -- down the tree
    start with manager_id in ( -- list up the tree
         select manager_id 
           from employees
         connect by employee_id = prior manager_id -- up the tree
         start with employee_id = 101
         union 
         select manager_id -- in case we are the root node
           from employees
         where manager_id = 101
         )
    ;
    

    Example for root node:

    select distinct employee_id, last_name, manager_id 
       from employees
    connect by prior employee_id = manager_id -- down the tree
    start with manager_id in ( -- list up the tree
         select manager_id 
           from employees
         connect by employee_id = prior manager_id -- up the tree
         start with employee_id = 100
         union 
         select manager_id -- in case we are the root node
           from employees
         where manager_id = 100
         )
    ;
    

    Fiddle at http://www.sqlfiddle.com/#!4/d15e7/32

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

Sidebar

Related Questions

given the following text file: Find all HeaderText=, Subfolders, Find Results 1, Entire Solution
I find the following example mildly surprising: >>> class Foo: def blah(self): pass >>>
Where can I find the entire source code for JDK 1.5 to download? I
I have searched my entire Mac for ibplugin to find the QTKit IBPlugin, but
Many times I find that I want to search my entire Visual Studio project
I come from an ASP.NET background and find the entire roles based authorization scheme
I'm trying to find out why this entire code below is producing only ONE
When I need to find the first node in a TTreeView , I call
I'm trying to find the average pixel color of the entire screen or section
I would like to do a find and replace inside an entire database not

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.