

<a id="flink-sql-with"></a>

# WITH Clause in Confluent Cloud for Apache Flink



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

## Syntax

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

<with_item_definition>:
   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, often called Common Table Expressions (CTEs),
are temporary views that exist for a single query.

## Example

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

```sql
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;
```

## Related content

- [Flink SQL Queries](overview.md#flink-sql-queries)
- [Flink SQL Functions](../functions/overview.md#flink-sql-functions-overview)
- [Statements](../statements/overview.md#flink-sql-statements-overview)

#### NOTE
This website includes content developed at the [Apache Software Foundation](https://www.apache.org/)
under the terms of the [Apache License v2](https://www.apache.org/licenses/LICENSE-2.0.html).
