CONNECT_BY_ROOT is a unary operator that you can use to qualify a column to return the column’s value of the row considered to be the root node in relation to the current row.
Note
A unary operator operates on a single operand. In the case of CONNECT_BY_ROOT, it's the column name following the CONNECT_BY_ROOT keyword.
Syntax
In the consql of the SELECT list, the CONNECT_BY_ROOT operator is shown by the following.
Some points to note about the CONNECT_BY_ROOT operator:
You can use the CONNECT_BY_ROOT operator in the SELECT list, WHERE clause, GROUP BY clause, HAVING clause, ORDER BY clause, and ORDER SIBLINGS BY clause as long as the SELECT command is for a hierarchical query.
You can't use the CONNECT_BY_ROOT operator in the CONNECT BY clause or the START WITH clause of the hierarchical query.
You can apply CONNECT_BY_ROOT to an expression involving a column. To do so, you must enclose the expression in parentheses.
Examples
The following query shows the use of the CONNECT_BY_ROOT operator to return the employee number and employee name of the root node for each employee listed in the result set based on trees starting with employees BLAKE, CLARK, and JONES.
The output from the query shows that all of the root nodes in columns mgr empno and mgr ename are one of the employees—BLAKE, CLARK, or JONES—listed in the START WITH clause.
A similar query but producing only one tree starting with the single, top-level employee where the mgr column is null:
In the following output, all of the root nodes in columns mgr empno and mgr ename indicate KING as the root for this query:
By contrast, this example omits the START WITH clause, thereby resulting in 14 trees:
In the output from the query, each node appears at least once as a root node under the mgr empno and mgr ename columns, since even the leaf nodes form the top of their own trees.
When applied to an expression that isn't enclosed in parentheses, the CONNECT_BY_ROOT operator affects only the term ename immediately following it. The subsequent concatenation of || ' manages ' || ename isn't part of the CONNECT_BY_ROOT operation. Hence the second occurrence of ename results in the value of the currently processed row, while the first occurrence of ename results in the value from the root node.
Note the values produced under the top mgr/employee column in the output from the query:
This example uses the CONNECT_BY_ROOT operator on an expression enclosed in parentheses:
The values of both ename and empno are affected by the CONNECT_BY_ROOT operator. As a result, they return the values from the root node under the top mgr/empno column: