Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How To Order Balanced Tree Using SQL Hierarchical Queries

BinepalOct 24 2011 — edited Oct 25 2011
while searching forum i found this

9588836

is there a way i can order this tree alphabetically so that result looks like

LEVEL INDENTED_ENAME
---------- --------------------
1 KING
2 BLAKE
3 ALLEN
3 JAMES
3 MARTIN
3 TURNER
3 WARD
2 CLARK
3 MILLER
2 JONES
3 FORD
4 SMITH
3 SCOTT
4 ADAMS

---------original query----------

SELECT LEVEL
, LPAD (' ', 3 * LEVEL) || ename AS indented_ename
FROM scott.emp
START WITH mgr IS NULL
CONNECT BY mgr = PRIOR empno
;

LEVEL INDENTED_ENAME
---------- --------------------
1 KING
2 JONES
3 SCOTT
4 ADAMS
3 FORD
4 SMITH
2 BLAKE
3 ALLEN
3 WARD
3 MARTIN
3 TURNER
3 JAMES
2 CLARK
3 MILLER
This post has been answered by Frank Kulash on Oct 24 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 22 2011
Added on Oct 24 2011
6 comments
348 views