WITH
constructsupportedQueries & DMLThe WITH clause is an optional clause that precedes the body of the SELECT statement, and defines one or more CTEs (common table expressions) that can be used later in the statement. For example, CTEs can be referenced in the FROM clause. Note You can use a WITH clause when creating and calling an anonymous procedure similar to a stored procedure. That clause modifies a CALL command rather than a SELECT command. For more information, see CALL (with anonymous procedure). The WITH clause is used with machine learning model objects to create an alias to a specific version of the model, which can then be used to call the methods of that version. See Calling model methods.
Syntax
supportedpartialnot supportednot scored
Subquery
[ WITH
<cte_name1> [ ( <cte_column_list> ) ] AS ( SELECT ... )
[ , <cte_name2> [ ( <cte_column_list> ) ] AS ( SELECT ... ) ]
[ , <cte_nameN> [ ( <cte_column_list> ) ] AS ( SELECT ... ) ]
]
SELECT ...
Recursive CTE
[ WITH [ RECURSIVE ]
<cte_name1> ( <cte_column_list> ) AS ( anchorClause UNION ALL recursiveClause )
[ , <cte_name2> ( <cte_column_list> ) AS ( anchorClause UNION ALL recursiveClause ) ]
[ , <cte_nameN> ( <cte_column_list> ) AS ( anchorClause UNION ALL recursiveClause ) ]
]
SELECT ...
Where
anchorClause ::=
SELECT <anchor_column_list> FROM ...
recursiveClause ::=
SELECT <recursive_column_list> FROM ... [ JOIN ... ]
Parameters
| Name | Required | Description |
|---|---|---|
cte_name1 , cte_nameN | The CTE name must follow the rules for views and similar object identifiers. | |
cte_column_list | The names of the columns in the CTE (common table expression). | |
anchor_column_list | The columns used in the anchor clause for the recursive CTE. The columns in this list must correspond to the columns defined in cte_column_list. | |
recursive_column_list | The columns used in the recursive clause for the recursive CTE. The columns in this list must correspond to the columns defined in cte_column_list. |
Usage notes
- A WITH clause can refer recursively to itself, and to other CTEs that appear earlier in the same clause. For instance, cte_name2 can refer to cte_name1 and itself, while cte_name1 can refer to itself, but not to cte_name2.
- You can mix recursive and non-recursive (iterative and non-iterative) CTE clauses in the WITH clause. The CTE clauses should be ordered such that, if a CTE needs to reference another CTE, the CTE to be referenced should be defined earlier in the statement (e.g. the second CTE can refer to the first CTE, but not vice versa). The CTEs do not need to be listed in order based on whether they are recursive or not. For example, a non-recursive CTE can be listed immediately after the keyword RECURSIVE, and a recursive CTE can come after that non-recursive CTE. Within a recursive CTE, either the anchor clause or the recursive clause (or both) can refer to another CTE(s).
- For recursive CTEs, the cte_column_list is required.
- For non-recursive CTEs, the cte_column_list is optional.
- Make sure to use UNION ALL, not UNION, in a recursive CTE.
- The keyword RECURSIVE is optional. CTEs can be recursive whether or not RECURSIVE was specified.
- You can use the keyword RECURSIVE even if no CTEs are recursive.
- If RECURSIVE is used, it must be used only once, even if more than one CTE is recursive.
- Although SQL statements work properly with or without the keyword RECURSIVE, using the keyword properly makes the code easier to understand and maintain. Snowflake recommends using the keyword RECURSIVE if one or more CTEs are recursive, and Snowflake strongly recommends omitting the keyword if none of the CTEs are recursive.
- Attention
- When using a recursive CTE, it is possible to create a query that goes into an infinite loop and consumes credits until the query succeeds, the query times out (e.g. exceeds the number of seconds specified by the STATEMENT_TIMEOUT_IN_SECONDS parameter), or you cancel the query.
- For information on how infinite loops can occur and for guidelines on how to avoid this problem, see Troubleshooting a Recursive CTE.
- For example, to limit the number of iterations to less than 10:
- The Snowflake implementation of recursive CTEs does not support the following keywords that some other systems support: SEARCH DEPTH FIRST BY ...
- CYCLE ... SET ...
- The anchor clause in a recursive CTE is a SELECT statement.
- The anchor clause is executed once during the execution of the statement in which it is embedded; it runs before the recursive clause and generates the first set of rows from the recursive CTE. These rows are not only included in the output of the query, but also referenced by the recursive clause.
- The anchor clause can contain any SQL construct allowed in a SELECT clause. However, the anchor clause cannot reference cte_name1; only the recursive clause can reference cte_name1.
- Although the anchor clause usually selects from the same table as the recursive clause, this is not required. The anchor clause can select from any table-like data source, including another table, a view, a UDTF, or a constant value.
- The anchor clause selects a single “level” of the hierarchy, typically the top level, or the highest level of interest. For example, if the query is intended to show the “parts explosion” of a car, the anchor clause returns the highest level component, which is the car itself.
- The output from the anchor clause represents one layer of the hierarchy, and this layer is stored as the content of the “view” that is accessed in the first iteration of the recursive clause.
- The recursive clause is a SELECT statement. This SELECT is restricted to projections, filters, and joins (inner joins and outer joins in which the recursive reference is on the preserved side of the outer join). The recursive clause cannot contain:
- Aggregate or window functions,
- GROUP BY, ORDER BY, LIMIT, or DISTINCT.
- The recursive clause can (and usually does) reference the cte_name1 as though the CTE were a table or view.
- The recursive clause usually includes a JOIN that joins the table that was used in the anchor clause to the CTE. However, the JOIN can join more than one table or table-like data source (view, etc.).
- The first iteration of the recursive clause starts with the data from the anchor clause. That data is then joined to the other table(s) in the FROM clause of the recursive clause.
- Each subsequent iteration starts with the data from the previous iteration.
- You can think of the CTE clause or “view” as holding the contents from the previous iteration, so that those contents are available to be joined. Note that during any one iteration, the CTE contains only the contents from the previous iteration, not the results accumulated from all previous iterations. The accumulated results (including from the anchor clause) are stored in a separate place.
- There are three column lists in a recursive CTE:
- cte_column_list
- anchor_column_list (in the anchor clause)
- recursive_column_list (in the recursive clause)
- A recursive CTE can contain other column lists (e.g. in a subquery), but these three column lists must be present.
- These three column lists must all correspond to each other.
- In pseudo-code, this looks similar to:
- Columns X and related_to_X must correspond; the anchor clause generates the initial “contents” of the “view” that the CTE represents, so each column from the anchor clause (e.g. column related_to_x) must generate output that will belong in the corresponding column of the CTE (e.g. column X).
- Columns also_related_to_X and X must correspond; on each iteration of the recursive clause, the output of that clause becomes the new content of the CTE/view for the next iteration.
- Also, columns related_to_X and also_related_to_X must correspond because they are each on one side of the UNION ALL operator, and the columns on each side of a UNION ALL operator must correspond.
Parameters
cte_name1 , cte_nameN: The CTE name must follow the rules for views and similar object identifiers. cte_column_list: The names of the columns in the CTE (common table expression). anchor_column_list: The columns used in the anchor clause for the recursive CTE. The columns in this list must correspond to the columns defined in cte_column_list. recursive_column_list: The columns used in the recursive clause for the recursive CTE. The columns in this list must correspond to the columns defined in cte_column_list. For more details, see Anchor Clause and Recursive Clause (in this topic). For a detailed explanation of how the anchor clause and recursive clause work together, see Working with CTEs (Common Table Expressions).