# Common table expressions

[Home](/rezoom-sql/master.md) > [Language](/rezoom-sql/language.md) > Common table expressions

[← Delete statements](/rezoom-sql/language/deletestmt.md) | [Create table statements →](/rezoom-sql/language/createtablestmt.md)

## Common table expressions

Sometimes it is necessary to use the same subquery multiple times within a complicated query.

A **common table expression**, or CTE for short, is the tool of choice for these situations.

#### *common-table-expression*

![](/files/dsM4DjrhvhVyBYOheuqJ)

Here's a trivial example:

```sql
with MyCte(x, y)
as (select 1, 2),
MyOtherCte(z)
as (select 3)
select * from MyCte join MyOtherCte on y + 1 = z
```

### Recursive CTEs

You can also write *recursive* CTEs, like this example [adapted from the SQLite documentation](https://sqlite.org/lang_with.html):

```sql
WITH RECURSIVE
  xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+0.05 FROM xaxis WHERE x<1.2),
  yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+0.1 FROM yaxis WHERE y<1.0),
  m(iter, cx, cy, x, y) AS (
    SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis
    UNION ALL
    SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m
    WHERE (x*x + y*y) < 4.0 AND iter<28
  ),
  m2(iter, cx, cy) AS (
    SELECT max(iter), cx, cy FROM m GROUP BY cx, cy
  ),
  a(t) AS (
    SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '')
    FROM m2 GROUP BY cy
  )
SELECT group_concat(rtrim(t),'CRLF') as x FROM a
```

See it [typecheck here](http://rzsql.net/#7B68353227CCB1A78FEA0172676CE5F3A208C1FA).

***

[← Delete statements](/rezoom-sql/language/deletestmt.md) | [Create table statements →](/rezoom-sql/language/createtablestmt.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://humbobst.gitbook.io/rezoom-sql/language/commontableexpression.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
