# Subqueries

{% columns %}
{% column %}
{% content-ref url="/pages/AviU4lFLCp4v9SVcybwW" %}
[Subqueries With ALL](/docs/server/reference/sql-statements/data-manipulation/selecting-data/subqueries/subqueries-and-all.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Compare a value against all results from a subquery. The `ALL` operator returns `TRUE` if the comparison holds for every row returned by the subquery.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/HgVb7wv784EUZZEGEznb" %}
[Subqueries With ANY and SOME](/docs/server/reference/sql-statements/data-manipulation/selecting-data/subqueries/subqueries-and-any.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Compare a value against any result from a subquery. The `ANY` (or `SOME`) operator returns `TRUE` if the comparison holds for at least one row. `IN` can be used for `=ANY`.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/lLtREPbZvVnrkqVx3Fet" %}
[Subqueries With EXISTS](/docs/server/reference/sql-statements/data-manipulation/selecting-data/subqueries/subqueries-and-exists.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Test for the existence of rows. The `EXISTS` operator returns `TRUE` if the subquery returns at least one row, often used for correlated subqueries.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/btd3F3kW9f98blWbV3OE" %}
[Subqueries With JOINs](/docs/server/reference/sql-statements/data-manipulation/selecting-data/subqueries/subqueries-and-joins.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Understand when to use subqueries versus joins. This guide explains performance implications and how to rewrite subqueries as joins for efficiency.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/UOpxCJiQoT2irvPtK9pF" %}
[Subqueries in a FROM Clause (Derived Tables)](/docs/server/reference/sql-statements/data-manipulation/selecting-data/subqueries/subqueries-in-a-from-clause-derived-tables.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Use a subquery as a temporary table. Derived tables allow you to select from the result set of another query within the `FROM` clause.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/POig64cbRhBaNgokN4y8" %}
[Row Subqueries](/docs/server/reference/sql-statements/data-manipulation/selecting-data/subqueries/subqueries-row-subqueries.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Retrieve a single row of multiple values. A row subquery returns a tuple that can be compared against a row constructor in the outer query.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/X2lcYDHOCjDxwLSEDufJ" %}
[Scalar Subqueries](/docs/server/reference/sql-statements/data-manipulation/selecting-data/subqueries/subqueries-scalar-subqueries.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Return a single value. A scalar subquery produces a one-row, one-column result that can be used anywhere a constant or expression is valid.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/r9NLkVkrZbE8XSx3JUgu" %}
[Subquery Limitations](/docs/server/reference/sql-statements/data-manipulation/selecting-data/subqueries/subquery-limitations.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Review restrictions on subquery usage. This page details unsupported operations, such as modifying a table while selecting from it in a subquery.
{% endcolumn %}
{% endcolumns %}


---

# 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://mariadb.com/docs/server/reference/sql-statements/data-manipulation/selecting-data/subqueries.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.
