2019-08-19 02:17:50 -04:00
---
layout: global
title: Common Table Expression (CTE)
displayTitle: Common Table Expression (CTE)
license: |
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
---
2020-04-15 19:34:26 -04:00
### Description
A common table expression (CTE) defines a temporary result set that a user can reference possibly multiple times within the scope of a SQL statement. A CTE is used mainly in a SELECT statement.
### Syntax
2020-05-10 13:57:25 -04:00
```sql
2020-04-15 19:34:26 -04:00
WITH common_table_expression [ , ... ]
2020-05-10 13:57:25 -04:00
```
2020-04-15 19:34:26 -04:00
While `common_table_expression` is defined as
2020-05-10 13:57:25 -04:00
```sql
expression_name [ ( column_name [ , ... ] ) ] [ AS ] ( query )
```
2020-04-15 19:34:26 -04:00
### Parameters
2020-05-10 13:57:25 -04:00
* **expression_name**
2020-04-15 19:34:26 -04:00
Specifies a name for the common table expression.
2020-05-10 13:57:25 -04:00
* **query**
A [SELECT statement ](sql-ref-syntax-qry-select.html ).
2020-04-15 19:34:26 -04:00
### Examples
2020-05-10 13:57:25 -04:00
```sql
2020-04-15 19:34:26 -04:00
-- CTE with multiple column aliases
WITH t(x, y) AS (SELECT 1, 2)
SELECT * FROM t WHERE x = 1 AND y = 2;
2020-05-01 13:11:43 -04:00
+---+---+
| x| y|
+---+---+
| 1| 2|
+---+---+
2020-04-15 19:34:26 -04:00
-- CTE in CTE definition
2020-05-10 13:57:25 -04:00
WITH t AS (
2020-04-15 19:34:26 -04:00
WITH t2 AS (SELECT 1)
SELECT * FROM t2
)
SELECT * FROM t;
2020-05-01 13:11:43 -04:00
+---+
| 1|
+---+
| 1|
+---+
2020-04-15 19:34:26 -04:00
-- CTE in subquery
SELECT max(c) FROM (
WITH t(c) AS (SELECT 1)
SELECT * FROM t
);
2020-05-01 13:11:43 -04:00
+------+
|max(c)|
+------+
| 1|
+------+
2020-04-15 19:34:26 -04:00
-- CTE in subquery expression
SELECT (
WITH t AS (SELECT 1)
SELECT * FROM t
);
2020-05-01 13:11:43 -04:00
+----------------+
|scalarsubquery()|
+----------------+
| 1|
+----------------+
2020-04-15 19:34:26 -04:00
-- CTE in CREATE VIEW statement
CREATE VIEW v AS
WITH t(a, b, c, d) AS (SELECT 1, 2, 3, 4)
SELECT * FROM t;
SELECT * FROM v;
2020-05-01 13:11:43 -04:00
+---+---+---+---+
| a| b| c| d|
+---+---+---+---+
| 1| 2| 3| 4|
+---+---+---+---+
2020-04-15 19:34:26 -04:00
-- If name conflict is detected in nested CTE, then AnalysisException is thrown by default.
-- SET spark.sql.legacy.ctePrecedencePolicy = CORRECTED (which is recommended),
-- inner CTE definitions take precedence over outer definitions.
SET spark.sql.legacy.ctePrecedencePolicy = CORRECTED;
WITH
t AS (SELECT 1),
t2 AS (
WITH t AS (SELECT 2)
SELECT * FROM t
)
SELECT * FROM t2;
2020-05-01 13:11:43 -04:00
+---+
| 2|
+---+
| 2|
+---+
2020-05-10 13:57:25 -04:00
```
2020-04-15 19:34:26 -04:00
### Related Statements
2020-05-10 13:57:25 -04:00
* [SELECT ](sql-ref-syntax-qry-select.html )