CONNECT BY
Description
The CONNECT BY
clause in Oracle is used for hierarchical queries, which are queries that deal with data that has a parent-child relationship, such as organizational structures, bill of materials, or tree-structured data. It allows to traverse the hierarchy and generate a result set that reflects the hierarchical structure of the data.
Components of CONNECT BY
CONNECT BY
PRIOR: This operator specifies the parent-child relationship in the hierarchy. It is used to indicate the column that holds the reference to the parent row.
LEVEL: A pseudo-column that returns the level number of a node in a tree structure. The root level is 1, the next level is 2, and so on.
START WITH: This clause specifies the root of the hierarchy, i.e., the starting point of the query.
SYS_CONNECT_BY_PATH: A function that returns the path of a node from the root.
CONNECT_BY_ROOT and CONNECT_BY_ISLEAF:
CONNECT_BY_ROOT
andCONNECT_BY_ISLEAF
are hierarchical query pseudocolumns used with theCONNECT BY
clause to navigate and manipulate hierarchical data structures, such as organizational charts or parts explosions. These pseudocolumns are especially useful when working with self-referential tables where a row refers to another row in the same table.
Syntax
CONNECT_BY_ROOT
The CONNECT_BY_ROOT
pseudocolumn is used to return the root row of the hierarchy from the current row. This means it retrieves the starting point (root) of the hierarchy for each row in the result set.
CONNECT_BY_ISLEAF
The CONNECT_BY_ISLEAF
pseudocolumn is used to indicate whether a row is a leaf node in the hierarchy. A leaf node is a node that does not have any children
Examples
Employee and Manager
Consider a table employees
with the following structure:
1
John
NULL
2
Mike
1
3
Sara
1
4
Kate
2
5
Paul
2
In this table:
EMPLOYEE_ID
is the unique identifier for each employee (child).MANAGER_ID
is the unique identifier for the manager of each employee (parent).
Hierarchical Query Using CONNECT BY
CONNECT BY
To retrieve the hierarchy starting from the top-level manager:
Explanation:
SELECT EMPLOYEE_ID, NAME, LEVEL, SYS_CONNECT_BY_PATH(NAME, '->') AS PATH: Selects the employee ID, name, hierarchical level, and the path from the root.
FROM employees: Specifies the source table.
START WITH MANAGER_ID IS NULL: Specifies the root of the hierarchy (employees with no manager).
CONNECT BY PRIOR EMPLOYEE_ID = MANAGER_ID: Establishes the parent-child relationship. Each employee's manager is specified by
MANAGER_ID
.
Result:
1
John
1
->John
2
Mike
2
->John->Mike
3
Sara
2
->John->Sara
4
Kate
3
->John->Mike->Kate
5
Paul
3
->John->Mike->Paul
LEVEL: Indicates the depth of the current row in the hierarchy. The root node has a LEVEL
of 1, its children have a LEVEL
of 2, and so on.
PRIOR: Used to refer to the parent row in the hierarchy. In PRIOR parent_column = child_column
, PRIOR
refers to the value in the parent row.
START WITH: Specifies the root row(s) of the hierarchy. If omitted, all rows are considered roots, which is usually not desired.
SYS_CONNECT_BY_PATH: Returns the path from the root to the current row, which is useful for displaying the hierarchy as a string.
Print difference of 2 dates into rows
Given 2 dates, print all the dates within those 2 dates including itself.
Display each employee along with their root manager
Display whether each employee is a leaf node in the hierarchy
Last updated
Was this helpful?