START WITH and CONNECT BY in Oracle

Your rating: None Average: 4 (40 votes)

Here is a quick example to help you understand how oracle Start With analytical function works. The start with .. connect by clause can be used to select data that has a hierarchical relationship. Usually, it is some sort of parent child relationship like supervisor and an employee.

Let’s assume we have a table called employees. I would like to know employees that directly report to supervisor Id 1122456 –simple, right?

SELECT emplid, supervisor_id
  FROM ps_employees
 WHERE supervisor_id = '1122456';

EMPLID  SUPERVISOR_ID
0119676 1122456
0112356 1122456
0120022 1122456

Now I would like to know all employees that report to supervisor Id 1122456 (directly or indirectly).

SELECT     emplid, supervisor_id
      FROM ps_employees
START WITH supervisor_id = '1122456'
CONNECT BY PRIOR emplid = supervisor_id;

EMPLID  SUPERVISOR_ID
0119676 1122456
0112356 1122456
0120022 1122456
0120033 0120022

Notice that with the 1st sql we had 3 rows total while using Start With gave us 4 rows back. If you look at the last row on the 2nd select, you notice that employee 0120033 reports to 0120022 who in return reports to 1122456.

Please try to help out with unanswered topics on the forum. Chances are you have had the same issue/question some time in your IT career!

Comments

Have a question? Please ask it on the forum instead.
Guest's picture
Guest (not verified)
Re: START WITH and CONNECT BY in Oracle

Very nicely explained

Guest's picture
Guest (not verified)
Re: START WITH and CONNECT BY in Oracle

Nice and simple explanation. Thanks.

Beto's picture
Beto (not verified)
Re: START WITH and CONNECT BY in Oracle

So good. Thanks

Guest's picture
Guest (not verified)
Re: START WITH and CONNECT BY in Oracle

Hi,

my problem with connect by is more difficult. Let say we have a table for bils of material decribing following structure:
prod
|
|-> semif1
| |
| |-> semif3
| | |
| | |->comp4
| |
| |-> comp2
|
|-> semif2
| |
| |->comp3
|
|-> comp1

where prod is final product, semifx are semifinished items on different levels in stucture and compx are components on different levels.
The table for this structure looks like follow:
prod mater
-----------------
prod semif1
prod semif2
prod comp1
semif1 semif3
semif1 comp2
semif2 comp3
semif3 comp4

there is also another table where are stored information about acqusition method of particular items. Let say acq. method for prod and all semifinished items is 1=manufactured and acq.method for components is 2=purchased.
What I would need is to exclude items semif3 and comp4 from result when acq.method for semif1 is changed on 2. Is somethink like this possible in simple SQL?

Jo's picture
Jo (not verified)
Re: START WITH and CONNECT BY in Oracle

This is awesome!!!
love the simple explanation!
Thanks heaps!

Stephen's picture
Stephen (not verified)
Re: START WITH and CONNECT BY in Oracle

Simple and straight to the point explanation, thanks.

Yuva Raja's picture
Yuva Raja (not verified)
Re: START WITH and CONNECT BY in Oracle

Awesome!!! u have done a great job.. Simple and effective explanation.. Keep it up...

Thanks

Sanjeev's picture
Sanjeev (not verified)
Re: START WITH and CONNECT BY in Oracle

Very simple and well explainaition.
Thanks a lot .

Guest's picture
Guest (not verified)
Re: START WITH and CONNECT BY in Oracle

Yes

kannadhasan's picture
User offline. Last seen 5 years 11 weeks ago. Offline
Joined: 06/10/2012
Posts: 4
Re: START WITH and CONNECT BY in Oracle

Nice Explanation.Thanks !!!

Guest's picture
Guest (not verified)
Re: START WITH and CONNECT BY in Oracle

good one

Guest's picture
Guest (not verified)
Re: START WITH and CONNECT BY in Oracle

gr8 explanation.. thanks buddy

Soundara Pandian Sundaram's picture
Soundara Pandian Sundaram (not verified)
Re: START WITH and CONNECT BY in Oracle

superrrrrr explaination... love this :)

Guest's picture
Guest
Re: START WITH and CONNECT BY in Oracle

After looking into many posts in various websites at last i got it here..

Guest's picture
Guest
Re: START WITH and CONNECT BY in Oracle

Really nice and simple explanation. Was struggling to understand this concept. And all other websites has more confusing explanations and examples. this one made this concept so clear and easy! Thanks a lot!!

Guest's picture
Guest
Re: START WITH and CONNECT BY in Oracle

itni saralta se samjhaya hai ke kya kahna....bahut sundar....very nicely explained. I was looking for this kind of explanation only....

Post new comment

CAPTCHA
The question below is to prevent automated spam submissions.
5 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.