Depending upon your use of the LEVEL column (as per my comment).
Info on Oracle Hierarchical Queries: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm
This returns what you ask for if LEVEL is the Oracle pseudocolumn:
WITH t AS (SELECT 0 AS parent,
1 AS id,
'toplevel' AS text FROM DUAL
UNION
SELECT 1 AS parent,
2 AS id,
'foo' AS text FROM DUAL
UNION
SELECT 1 AS parent,
3 AS id,
'sumthin else' AS text FROM DUAL
UNION
SELECT 1 AS parent,
4 AS id,
'foo' AS text FROM DUAL
UNION
SELECT 0 AS parent,
7 AS id,
'toplevel2' AS text FROM DUAL
UNION
SELECT 7 AS parent,
8 AS id,
'secondlevel' AS text FROM DUAL
UNION
SELECT 7 AS parent,
9 AS id,
'anothersecondlevel' AS text FROM DUAL
)
SELECT UNIQUE
level,
parent,
id,
text
FROM t
START WITH text = 'foo'
CONNECT BY PRIOR parent = id
ORDER BY parent;
Returns:
LEVEL PARENT ID TEXT
2 0 1 toplevel
1 1 2 foo
1 1 4 foo
If LEVEL is a column in your table then:
WITH t AS (SELECT 0 AS tlevel,
0 AS parent,
1 AS id,
'toplevel' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
1 AS parent,
2 AS id,
'foo' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
1 AS parent,
3 AS id,
'sumthin else' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
1 AS parent,
4 AS id,
'foo' AS text FROM DUAL
UNION
SELECT 0 AS tlevel,
0 AS parent,
7 AS id,
'toplevel2' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
7 AS parent,
8 AS id,
'secondlevel' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
7 AS parent,
9 AS id,
'anothersecondlevel' AS text FROM DUAL
)
SELECT UNIQUE
tlevel,
parent,
id,
text
FROM t
START WITH text = 'foo'
CONNECT BY PRIOR parent = id
ORDER BY parent;
Returns:
TLEVEL PARENT ID TEXT
0 0 1 toplevel
1 1 2 foo
1 1 4 foo
Hope it helps...