---
cuppy_hub: salesforce
cuppy_path: reference/official/data360/data-cloud-query-guide/guide/common-table-expressions.md
cuppy_section: docs
cuppy_doc: DNYY-NVW2
cuppy_doc_uuid: fb794fba-7723-46cc-a904-4b174da5b016
cuppy_permalink: https://cuppy.io/salesforce/d/DNYY-NVW2
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/guide/common-table-expressions
cuppy_mcp: https://cuppy.io/mcp
cuppy_body_sha256: 0f223f2c902da07b2339b831a4772cafbe990f3ffff70c320a23a102225cc9d7
# 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/guide/common-table-expressions.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.

## Simplify Complex Queries with Common Table Expressions

Common Table Expressions (CTEs) are a powerful feature that allows you to create temporary named result sets within your query. Think of them as temporary tables that exist only for the duration of your query execution. CTEs make complex queries more readable and help you break down complicated logic into manageable pieces.

A CTE is defined using the `WITH` clause followed by a name and a query. Here’s the basic structure:

Let’s say you want to analyze high-value customers from your Individual DMO. First, create a CTE to identify customers with more than 5 contact points, then use that CTE in your main query:

This query creates a temporary result set called `high_activity_customers` and then selects from it in the main query.

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

*   **Improved Readability**: Break complex queries into logical, named components
*   **Reusability**: Reference the same subquery multiple times without repeating code
*   **Debugging**: Test each CTE individually to isolate issues

For comprehensive syntax details, advanced features like recursive CTEs, and additional examples, see the complete [WITH Clause reference](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/select-with.html).

CTEs are particularly useful when you need to:

*   Calculate intermediate results that you’ll use multiple times
*   Make complex joins more readable
*   Replace complex subqueries with named, reusable components
*   Perform multi-step data transformations

In the next sections, you’ll learn about other advanced querying techniques that work well with CTEs, such as [subqueries](https://developer.salesforce.com/docs/data/data-cloud-query-guide/guide/subqueries-related-data.html) and [joins](https://developer.salesforce.com/docs/data/data-cloud-query-guide/guide/query-joins.html).


---
<!-- 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 -->
