WITH clause
Description
A
WITHclause is an optional clause that precedes the SELECT list in a query. TheWITHclause defines one or more common_table_expressions. Each common table expression (CTE) defines a temporary table, which is similar to a view definition. You can reference these temporary tables in theFROMclause. (Redshift SQL Language Reference WITH Clause)
The WITH clause is fully supported in Snowflake.
Grammar Syntax
[ WITH [RECURSIVE] common_table_expression [, common_table_expression , ...] ]
--Where common_table_expression can be either non-recursive or recursive.
--Following is the non-recursive form:
CTE_table_name [ ( column_name [, ...] ) ] AS ( query )
--Following is the recursive form of common_table_expression:
CTE_table_name (column_name [, ...] ) AS ( recursive_query )Sample Source Patterns
Recursive form
Input Code:
101
John
100
110
Liu
101
102
Jorge
101
103
Kwaku
101
201
Sofía
102
106
Mateo
102
105
Richard
103
104
Paulo
103
110
Nikki
103
205
Zhang
104
120
Saanvi
104
200
Shirley
104
Output Code:
101
John
100
102
Jorge
101
103
Kwaku
101
110
Liu
101
106
Mateo
102
201
Sofía
102
110
Nikki
103
104
Paulo
103
105
Richard
103
120
Saanvi
104
200
Shirley
104
205
Zhang
104
Non recursive form
Input Code:
Carlos
null
John
Carlos
Jorge
John
Kwaku
John
Liu
John
Mateo
Jorge
Sofía
Jorge
Nikki
Kwaku
Paulo
Kwaku
Richard
Kwaku
Saanvi
Paulo
Shirley
Paulo
Zhang
Paulo
Output Code:
John
Carlos
Jorge
John
Kwaku
John
Liu
John
Mateo
Jorge
Sofía
Jorge
Nikki
Kwaku
Paulo
Kwaku
Richard
Kwaku
Saanvi
Paulo
Shirley
Paulo
Zhang
Paulo
Carlos
null
Related EWIs
There are no known issues.
Last updated
