Use Of ‘Connect By’ clause for Hierarchal Query

The Connect By clause is useful for tables which have 2 or more columns with parent-child relationships. While displaying rows, we can use ‘LEVEL’ pseudo column to indent the results as hierarchy.

Consider the following example of a community table:

 Community_IdParent_IdCommunity_Nametmr>Select * from Communities;COMMUNITY_ID  PARENT_ID COMMUNITY_NAME------------ ---------- -------------------------           1          0 TV Entertainment           2          1 NBC           3          1 ABC           4          1 FOX           5          2 Sports           6          2 Movies           7          3 Talk Show           8          3 Documentary           9          7 Educational          10          7 Fun10 rows selected./* Find all children, all leaf lets From Root*/ SELECT LPAD(' ',3*(LEVEL-1)) || substr(Community_Name, 1, 30) Community ,         Community_id, Parent_Id FROM   Communities CONNECT BY PRIOR Community_id = Parent_Id START WITH Parent_id= 0Results:COMMUNITY                           COMMUNITY_ID  PARENT_ID----------------------------------- ------------ ----------TV Entertainment                               1          0   NBC                                         2          1      Sports                                   5          2      Movies                                   6          2   ABC                                         3          1      Talk Show                                7          3         Educational                           9          7         Fun                                  10          7      Documentary                              8          3   FOX                                         4          110 rows selected./* Find immediate children For Parent_Id = 1 : Just go 1 level Down  */ SELECT LPAD(' ',3*(LEVEL-1)) || substr(Community_Name, 1, 30) Community ,         Community_id, Parent_Id FROM   Communities CONNECT BY Community_Id = PRIOR  Parent_Id START WITH Parent_Id= 1Results:COMMUNITY                           COMMUNITY_ID  PARENT_ID----------------------------------- ------------ ----------NBC                                            2          1   TV Entertainment                            1          0ABC                                            3          1   TV Entertainment                            1          0FOX                                            4          1   TV Entertainment                            1          06 rows selected./* Start At Node Community_Id = 7: Traverse up all the way till root */ SELECT LPAD(' ',3*(LEVEL-1)) || substr(Community_Name, 1, 30) Community ,         Community_id, Parent_Id FROM   Communities CONNECT BY Community_id = PRIOR  Parent_Id START WITH Community_id = 7Results:COMMUNITY                           COMMUNITY_ID  PARENT_ID----------------------------------- ------------ ----------Talk Show                                      7          3   ABC                                         3          1      TV Entertainment                         1          0/* Find all children, upto some LEVEL */ SELECT LPAD(' ',3*(LEVEL-1)) || substr(Community_Name, 1, 30) Community ,         Community_id, Parent_Id FROM   Communities Where  LEVEL <= 2 CONNECT BY PRIOR Community_Id = Parent_Id START WITH Parent_Id= 0


Results:

 COMMUNITY                           COMMUNITY_ID  PARENT_ID----------------------------------- ------------ ----------TV Entertainment                               1          0   NBC                                         2          1   ABC                                         3          1   FOX                                         4          1

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a