WITH clause must be defined before it’s used in the query.
Here is the basic syntax of a SELECT statement with the optional WITH clause:
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
The WITH clause provides a way to write supplemental statements for a larger query. These statements, also known as Common Table Expressions or CTEs, can be viewed as defining temporary tables that exist just for one query.
WITH clause must be defined before it’s used in the query.
Here is the basic syntax of a SELECT statement with the optional WITH clause:
WITH name_for_summary_data AS (SELECT statement)
SELECT columns
FROM name_for_summary_data
-- create a CTE called "cte" and use it in the main query
WITH cte AS (SELECT 35 AS x)
SELECT * FROM cte;
┌────┐
│ x │
├────┤
│ 35 │
└────┘
-- create two CTEs, where the second CTE references the first CTE
WITH cte AS (SELECT 35 AS i),
cte2 AS (SELECT i*100 AS x FROM cte)
SELECT * FROM cte2;
┌──────┐
│ x │
├──────┤
│ 3500 │
└──────┘
Was this page helpful?