﻿---
title: Combine and reuse ES|QL queries
description: ES|QL provides several ways to combine, filter, and reuse query results beyond querying a single index. Choose the mechanism that best fits your goal...
url: https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-combine-reuse-queries
products:
  - Elasticsearch
applies_to:
  - Elastic Cloud Serverless: Generally available
  - Elastic Stack: Generally available
---

# Combine and reuse ES|QL queries
ES|QL provides several ways to combine, filter, and reuse query results beyond querying a single index. Choose the mechanism that best fits your goal.

| Mechanism                                                                                                      | What it does                                                                                                                                                                                                                                                                                                        | When to use                                                           |
|----------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------|
| [Subquery](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-subquery) | Nests a query inside another query, either to [combine result sets](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-from-subquery) or to [filter or evaluate rows](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-in-subquery) | You need to use the results of one query inside another               |
| [View](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-views)        | Saves a named query as a virtual index that any `FROM` can reference                                                                                                                                                                                                                                                | You want to define a query once and reuse it across multiple requests |
| [`FORK`](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/commands/fork)   | Sends the same incoming rows through multiple independent branches                                                                                                                                                                                                                                                  | You want to run different processing on the same data in one query    |


## Comparing views, subqueries, and FORK

[Views](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-views),
[`FROM` subqueries](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-from-subquery),
[`IN` subqueries](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-in-subquery), and
[`FORK`](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/commands/fork) share several traits but differ in important ways.
`IN` subqueries operate differently from the other three. They filter rows rather than producing branches, so the similarities and differences below apply to `FROM` subqueries, views, and `FORK` only.

### Similarities

- **Dynamic execution.** All three run at query time, so results reflect the current state of the data.
- **Union of columns.** Columns from multiple branches are merged into a single table. Missing columns are filled with `null` values.
- **Supported commands.** Complex processing commands can be used inside both views and `FROM` subqueries, as detailed in the [description of `FROM` subqueries](/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-from-subquery#description).
- **Nested branching.** Support depends on the version.

<applies-switch>
  <applies-item title="{ "stack": "ga 9.6+", "serverless": "ga" }" applies-to="Elastic Cloud Serverless: Generally available, Elastic Stack: Planned">
    Views, `FROM` subqueries, and `FORK` can be nested. The complete query
    remains subject to the branch and command limits.
  </applies-item>

  <applies-item title="stack: preview 9.4-9.5" applies-to="Elastic Stack: Preview from 9.4 to 9.5">
    Nested branching is generally not supported. Views can use
    [query compaction](/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-views#query-compaction)
    when the nested branches can be flattened into one branch point.
  </applies-item>
</applies-switch>

- **Branch limits.** The limits depend on the branching mechanism and version:

<applies-switch>
  <applies-item title="{ "stack": "ga 9.6+", "serverless": "ga" }" applies-to="Elastic Cloud Serverless: Generally available, Elastic Stack: Planned">
    - **Maximum branch count.** A single `FORK` is capped at 8 branches by default
      (`esql.query.max_branch_count_per_merge`).
    - View
      [query compaction](/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-views#query-compaction)
      flattens nested view unions only when the flat width is within
      `esql.query.max_branch_count` (20 by default). The same setting caps nested
      leaves across the whole query.
    - **Maximum branch level.** Nested branch points created by views, `FROM`
      subqueries, and `FORK` are capped at 5 levels by default
      (`esql.query.max_branch_level`). This limit counts nested branch points, not
      view nesting depth.
  </applies-item>

  <applies-item title="stack: preview 9.1-9.3, ga 9.4-9.5" applies-to="Elastic Stack: Generally available from 9.4 to 9.5, Elastic Stack: Preview from 9.1 to 9.3">
    - **Maximum branch count.** `FORK` is capped at 8 branches.
    - <applies-to>Elastic Stack: Preview from 9.4 to 9.5</applies-to> Views are capped at 8 branches.
  </applies-item>
</applies-switch>


### How FORK differs

- `FORK` does not include a source command. Every branch receives the same incoming rows and columns.
- `FROM` subqueries and views each have their own source command (`FROM`, `TS`, or `ROW`), so branches can read from different sources with different columns.

<applies-switch>
  <applies-item title="{ "stack": "ga 9.6+", "serverless": "ga" }" applies-to="Elastic Cloud Serverless: Generally available, Elastic Stack: Planned">
    A query can use multiple `FORK` commands when views or multi-source `FROM`
    commands create separate source branches, but each uninterrupted pipeline can
    contain only one. Refer to the
    [`FORK` limitations](/elastic/docs-builder/docs/4572/reference/query-languages/esql/commands/fork#limitations)
    for supported patterns and examples.
  </applies-item>

  <applies-item title="stack: preview 9.1-9.3, ga 9.4-9.5" applies-to="Elastic Stack: Generally available from 9.4 to 9.5, Elastic Stack: Preview from 9.1 to 9.3">
    Only one `FORK` command is allowed per query, so nested branches aren't possible.
    `FROM` subqueries and views have similar restrictions, but views can sometimes
    flatten nested branches through
    [query compaction](/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-views#query-compaction).
  </applies-item>
</applies-switch>


### How views differ from FROM subqueries

- Views must be defined using the [REST API](/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-views#create-and-manage-views) before they can be used in queries. Subqueries are written inline and require no setup.
- Views have names that are unique within the index namespace. A view cannot share a name with an index.
- Views can be nested up to a depth of 10. Cyclic references are not allowed. For example, if `viewA` references `viewB` and `viewB` references `viewC`, then `viewC` cannot reference `viewA`. Cycles are detected at query time.

<applies-switch>
  <applies-item title="{ "stack": "ga 9.6+", "serverless": "ga" }" applies-to="Elastic Cloud Serverless: Generally available, Elastic Stack: Planned">
    `FROM` subqueries and views can contain further `FROM` subqueries and `FORK`
    commands, subject to the branching limits.
  </applies-item>

  <applies-item title="stack: preview 9.4-9.5" applies-to="Elastic Stack: Preview from 9.4 to 9.5">
    No more than one branch point can exist across a view nesting chain. `FROM`
    subqueries don't support further `FROM` subqueries or `FORK` inside them, but
    they can contain `IN` subqueries. Views allow nested branching under
    [limited conditions](/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-views#combining-branching-and-nesting).
  </applies-item>
</applies-switch>


## Related alternatives

Depending on your goal, one of these alternatives may be a better fit:
- [`LOOKUP JOIN`](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-lookup-join): enrich rows by joining against a lookup index on a key field.
- [`ENRICH`](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-enrich-data): augment rows with data from an enrich policy.
- [Query multiple indices](https://www.elastic.co/elastic/docs-builder/docs/4572/reference/query-languages/esql/esql-multi-index): use comma-separated index patterns or wildcards in a single `FROM` to query across indices without subqueries.