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
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.
WITH common_table_expression [ , ... ]
While common_table_expression
is defined as
expression_name [ ( column_name [ , ... ] ) ] [ AS ] ( query )
expression_name
Specifies a name for the common table expression.
query
-- CTE with multiple column aliases WITH t(x, y) AS (SELECT 1, 2) SELECT * FROM t WHERE x = 1 AND y = 2; +---+---+ | x| y| +---+---+ | 1| 2| +---+---+ -- CTE in CTE definition WITH t AS ( WITH t2 AS (SELECT 1) SELECT * FROM t2 ) SELECT * FROM t; +---+ | 1| +---+ | 1| +---+ -- CTE in subquery SELECT max(c) FROM ( WITH t(c) AS (SELECT 1) SELECT * FROM t ); +------+ |max(c)| +------+ | 1| +------+ -- CTE in subquery expression SELECT ( WITH t AS (SELECT 1) SELECT * FROM t ); +----------------+ |scalarsubquery()| +----------------+ | 1| +----------------+ -- 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; +---+---+---+---+ | a| b| c| d| +---+---+---+---+ | 1| 2| 3| 4| +---+---+---+---+ WITH t AS (SELECT 1), t2 AS ( WITH t AS (SELECT 2) SELECT * FROM t ) SELECT * FROM t2; +---+ | 2| +---+ | 2| +---+