Loading section...
CTEs (WITH clause)
Concepts: sqlCte
A CTE is a named result set you define once at the top of a query, then reference by name anywhere below it. This works exactly the way you assign an intermediate variable in code before using it in a calculation. The filter logic lives inside the CTE, the main query reads from the named result, and the two steps stay cleanly separated. If you have ever computed something once to avoid repeating the same expression twice, you already understand what a CTE does. CTE Usage Examples The following examples show how to define and use CTEs, from a single named result set to chaining multiple CTEs together. Basic Example Multiple CTEs You can define several CTEs in a single query, separating them with commas. Each CTE can reference the ones declared before it, allowing you to build complex logic