Professional Documents
Culture Documents
Examples
Hierarchical queries :
I have explained lot of advanced sql topics like materialized view in
sql , partitioning techniques in oracle. In this article i will try to explain
different hierarchical queries in oracle sql with real life
examples.Hierarchical queries are useful in reporting purpose and
when user wants to fetch the data from hierarchical structure these
queries are very useful.Before going to the details of hierarchical
queries first question comes to the mind is ‘What is mean by
Hierarchy?’.
Hierarchy is nothing but the parent child relationship
within same table or the view
Hierarchical queries are very traditional and one of the useful feature
in day to day life.Every company has hierarchical data.The traversing
of the hierarchy is bottom up or top down.
Examples :
Bottom up hierarchy :
If user wants to find the top hierarchy of employee then bottom up
hierarchy is used.
In this section i will try give the real life example of the concept.To
write the query the data in the table should be in hierarchical
structure.
Step 1 :
Create the data in hierarchical structure
create table Employee_M
(Ename varchar2(20),employee_id varchar2(20),manager_id
varchar2(20));
Output :
2.START WITH clause is used to find out from which record user needs
to start the hierarchy. In above example we have started hierarchy with
the employee whose manager id is null.
3.LEVEL is a pseudocolumn which gives user the idea about the depth
of the hierarchy.
4.CONNECT BY gives user the idea about the first sibling record with
the specified condition.
In that case oracle will not create the hierarchy for the query. So to
manage that oracle has provided NOCYCLE clause.
(NOTE: I took the liberty to convert my personal code and tables to the
training Employee tables, some syntax or names may not be 100%
accurate.)
declare
employeeID varchar2(40) := ‘100’;
managerID varchar2(40) := ‘100’;
rec_empList myListOfEmp%rowtype; — I create a table with a list
of specific employees I wish to report, just has Emp_ID
cursor myList is
select emp_ID from myListOfEmp;
begin
open myList;
<>
fetch myList into rec_empList;
if myList%NOTFOUND then
go to CloseMyList;
end if;
employeeID := rec_MyList.Emp_ID;
goto NextRec;
<>
close myList;
end;
In above examples i explained the hierarchical queries with different
examples.Hope you like this article.
1. utsab says:
September 27, 2018 at 10:35 am
when an employee is his boss , then hierarchy output shows
Rakesh and Anmol having manager name as null ,
Reply
1. Amit S says:
September 27, 2018 at 3:01 pm
Reply
2. Mounika says:
December 14, 2018 at 3:28 pm
very easy to understand with good example
Reply
1. Amit S says:
December 14, 2018 at 3:40 pm
Thanks Mounika for appreciation!
Reply
3. Lakshmi says:
January 13, 2019 at 7:25 am
Good job and very well showcased!! Appreciate it !!
Reply
1. Amit S says:
January 15, 2019 at 1:17 pm
Thanks Lakshmi for good words!!!
Reply
4. Rohini says:
March 11, 2019 at 10:10 pm
great !, these sqls helped to meet the requirement
Reply
1. Amit S says:
March 13, 2019 at 5:06 pm
Thanks Rohini for good words…:)
Reply
declare
employeeID varchar2(40) := ‘100’;
managerID varchar2(40) := ‘100’;
rec_empList myListOfEmp%rowtype; — I create a table with a list of
specific employees I wish to report, just has Emp_ID
cursor myList is
select emp_ID from myListOfEmp;
begin
open myList;
<>
fetch myList into rec_empList;
if myList%NOTFOUND then
go to CloseMyList;
end if;
employeeID := rec_MyList.Emp_ID;
goto NextRec;
<>
close myList;
end;
Reply
1. Amit S says:
May 14, 2019 at 11:31 am
added your views of Hierarchical query..
Reply
6. Aniket says:
May 17, 2019 at 12:51 pm
How to use connect by level in query
Reply
1. Amit S says:
May 31, 2019 at 1:00 pm
If you go through this article you will get to know how to use
this..
Reply