---
cuppy_hub: salesforce
cuppy_path: reference/official/data360/data-cloud-query-guide/references/dc-sql-reference/select-with.md
cuppy_section: docs
cuppy_doc: AQXP-ACE3
cuppy_doc_uuid: 4f362410-5aed-471e-91d4-87a0b089a54b
cuppy_permalink: https://cuppy.io/salesforce/d/AQXP-ACE3
cuppy_version: 1
cuppy_updated: 2026-10-23T16:16:52.575Z
cuppy_canonical: https://cuppy.io/salesforce/docs/reference/official/data360/data-cloud-query-guide/references/dc-sql-reference/select-with
cuppy_mcp: https://cuppy.io/mcp
cuppy_body_sha256: 1f6bfaf6a3d4c64ef8ce942dacd0ca1cc4d2ff22c66a36bad41f79c144225511
# cuppy_body_sha256 is over the document body ALONE, excluding both generated blocks.
---

> Mirror of <https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/select-with.html> (Salesforce, Data 360 Query Guide), read 2026-10-23. Salesforce's own page is the authority; this copy exists because that site refuses non-browser readers.

## `WITH` Clause

The `WITH` clause allows you to create and use one or more subqueries. The subqueries act as temporary tables or views. Each subquery can be a [`SELECT`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/select.html) or [`VALUES`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/values.html) statement.

`WITH` queries evaluate one time per execution of the primary query, even if the primary query refers to them more than one time. The primary query and `WITH` queries execute at the same time.

To create a `WITH` clause, specify a name. Optionally, you can provide a list of column names. If you don’t provide column names, the column names are inferred from the subquery.

`RECURSIVE` allows a `SELECT` subquery to reference itself by name. The subquery syntax is `<non_recursive_term> UNION [ ALL | DISTINCT ] <recursive_term>`. Use the recursive self-reference on the right side of the `UNION`. Only one recursive self-reference is allowed per query. Make sure that the recursive part of the query eventually returns no tuples, or the query loops indefinitely.

where `with_query` is:

This example creates a simple CTE to filter and aggregate data before using it in the main query.

You can define multiple CTEs in a single query by separating them with commas.

This example shows how to specify column names explicitly in the CTE definition.

CTEs can also use VALUES statements to create temporary data sets.

This example demonstrates using UNION within a CTE to combine data from multiple sources.

This example uses `WITH RECURSIVE` to find all direct or indirect subordinates of employee Mary, and their level of indirectness, from a table that shows only direct subordinates.


---
<!-- cuppy -->
You are reading one file. The same content is free over MCP at https://cuppy.io/mcp with no signup: ranked search across all 376 documents in this hub, the merged view when you work in a layer, known workarounds matched to the error you hit, and depth control so you spend tokens on what you need. Start with the `_hello` tool. How to connect: https://cuppy.io/salesforce/ Something wrong in this document? Report it with the `session feedback` tool on the same endpoint.
<!-- /cuppy -->
