CONNECT BY

constructsupportedQueries & DML

docs.snowflake.com ↗

Joins a table to itself to process hierarchical data in the table. The CONNECT BY subclause of the FROM clause iterates to process the data. For example, you can create a query that shows a “parts explosion” to recursively list a component and the sub-components of that component. The Snowflake syntax for CONNECT BY is mostly compatible with the Oracle syntax.

Syntax

supportedpartialnot supportednot scored

The general form of a statement with CONNECT BY is similar to the following (some variations in order are allowed, but are not shown)

SELECT <column_list> [ , <level_expression> ]
  FROM <data_source>
    START WITH <predicate>
    CONNECT BY [ PRIOR ] <col1_identifier> = [ PRIOR ] <col2_identifier>
           [ , [ PRIOR ] <col3_identifier> = [ PRIOR ] <col4_identifier> ]
           ...
  ...

Parameters

NameRequiredDescription
column_listThis generally follows the rules for the projection clause of a SELECT statement.
level_expressionCONNECT BY queries allow some pseudo-columns. One of those pseudo-columns is LEVEL, which indicates the current level of the hierarchy (where level 1 represents the top of the hierarchy). The projection clause of the query can use LEVEL as a column.
data_sourceThe data source is usually a table, but can be another table-like data source, such as a view, UDTF, etc.
predicateThe predicate is an expression that selects the first “level” of the hierarchy (e.g. the president of the company or the top-level component in a parts explosion). The predicate should look similar to a WHERE clause, but without the keyword WHERE. See the Examples section (in this topic) for predicate examples.
colN_identifierThe CONNECT BY clause should contain one or more expressions similar to those used in joins. Specifically, a column in the “current” level of the table should refer to a column in the “prior” (higher) level of the table. For example, in a manager/employee hierarchy, the clause might look similar to:Copy code ... CONNECT BY manager_ID = PRIOR employee_ID ... The keyword PRIOR indicates that the value should be taken from the prior (higher/parent) level. In this example, the current employee’s manager_ID should match the prior level’s employee_ID. The CONNECT BY clause can contain more than one such expression, for example:Copy code ... CONNECT BY y = PRIOR x AND b = PRIOR a ... Each expression similar to the following should have exactly one occurrence of the keyword PRIOR:Copy code CONNECT BY <col_1_identifier> = <col_2_identifier> The keyword PRIOR may be on either the left-hand or right-hand side of the = sign. For example:Copy code CONNECT BY <col_1_identifier> = PRIOR <col_2_identifier> orCopy code CONNECT BY PRIOR <col_1_identifier> = <col_2_identifier>

Usage notes

Parameters

column_list: This generally follows the rules for the projection clause of a SELECT statement. level_expression: CONNECT BY queries allow some pseudo-columns. One of those pseudo-columns is LEVEL, which indicates the current level of the hierarchy (where level 1 represents the top of the hierarchy). The projection clause of the query can use LEVEL as a column. data_source: The data source is usually a table, but can be another table-like data source, such as a view, UDTF, etc. predicate: The predicate is an expression that selects the first “level” of the hierarchy (e.g. the president of the company or the top-level component in a parts explosion). The predicate should look similar to a WHERE clause, but without the keyword WHERE. See the Examples section (in this topic) for predicate examples. colN_identifier: The CONNECT BY clause should contain one or more expressions similar to those used in joins. Specifically, a column in the “current” level of the table should refer to a column in the “prior” (higher) level of the table. For example, in a manager/employee hierarchy, the clause might look similar to:Copy code ... CONNECT BY manager_ID = PRIOR employee_ID ... The keyword PRIOR indicates that the value should be taken from the prior (higher/parent) level. In this example, the current employee’s manager_ID should match the prior level’s employee_ID. The CONNECT BY clause can contain more than one such expression, for example:Copy code ... CONNECT BY y = PRIOR x AND b = PRIOR a ... Each expression similar to the following should have exactly one occurrence of the keyword PRIOR:Copy code CONNECT BY <col_1_identifier> = <col_2_identifier> The keyword PRIOR may be on either the left-hand or right-hand side of the = sign. For example:Copy code CONNECT BY <col_1_identifier> = PRIOR <col_2_identifier> orCopy code CONNECT BY PRIOR <col_1_identifier> = <col_2_identifier>

See also

Referenced by