WITH Clause

Important

Confluent Cloud for Apache Flink®️ is currently available for Preview. A Preview feature is a Confluent Cloud component that is being introduced to gain early feedback from developers. Preview features can be used for evaluation and non-production testing purposes or to provide feedback to Confluent. The warranty, SLA, and Support Services provisions of your agreement with Confluent do not apply to Preview features. Confluent may discontinue providing Preview releases of the Preview features at any time in Confluent’s sole discretion. Check out Getting Help for questions, feedback and requests.

For SQL features and limitations in the preview program, see Notable Limitations in Public Preview.

Confluent Cloud for Apache Flink®️ enables writing auxiliary statements to use in larger SQL queries.

Syntax

WITH <with_item_definition> [ , ... ]
SELECT ... FROM ...;

<with_item_defintion>:
   with_item_name (column_name[, ...n]) AS ( <select_query> )

Description

The WITH clause provides a way to write auxiliary statements for use in a larger query. These statements, which are often referred to as Common Table Expressions (CTE), can be thought of as defining temporary views that exist just for one query.

Example

The following example defines a common table expression orders_with_total and uses it in a GROUP BY query.

WITH orders_with_total AS (
    SELECT order_id, price + tax AS total
    FROM orders
)
SELECT order_id, SUM(total)
FROM orders_with_total
GROUP BY order_id;