### 13.2.11 Subqueries

[13.2.11.1 The Subquery as Scalar Operand](scalar-subqueries.html)
[13.2.11.2 Comparisons Using Subqueries](comparisons-using-subqueries.html)
[13.2.11.3 Subqueries with ANY, IN, or SOME](any-in-some-subqueries.html)
[13.2.11.4 Subqueries with ALL](all-subqueries.html)
[13.2.11.5 Row Subqueries](row-subqueries.html)
[13.2.11.6 Subqueries with EXISTS or NOT EXISTS](exists-and-not-exists-subqueries.html)
[13.2.11.7 Correlated Subqueries](correlated-subqueries.html)
[13.2.11.8 Derived Tables](derived-tables.html)
[13.2.11.9 Lateral Derived Tables](lateral-derived-tables.html)
[13.2.11.10 Subquery Errors](subquery-errors.html)
[13.2.11.11 Optimizing Subqueries](optimizing-subqueries.html)
[13.2.11.12 Restrictions on Subqueries](subquery-restrictions.html)

[]()[]()[]()[]()

 A subquery is a [`SELECT`](select.html) statement within another statement.

 All subquery forms and operations that the SQL standard requires are supported, as well as a few features that are MySQL-specific.

 Here is an example of a subquery:

```
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
```

 In this example, `SELECT * FROM t1 ...` is the *outer query* (or *outer statement*), and `(SELECT column1 FROM t2)` is the *subquery*. We say that the subquery is *nested* within the outer query, and in fact it is possible to nest subqueries within other subqueries, to a considerable depth. A subquery must always appear within parentheses.

 The main advantages of subqueries are:

* They allow queries that are *structured* so that it is possible to isolate each part of a statement.

* They provide alternative ways to perform operations that would otherwise require complex joins and unions.

* Many people find subqueries more readable than complex joins or unions. Indeed, it was the innovation of subqueries that gave people the original idea of calling the early SQL “Structured Query Language.”

 Here is an example statement that shows the major points about subquery syntax as specified by the SQL standard and supported in MySQL:

```
DELETE FROM t1
WHERE s11 > ANY
 (SELECT COUNT(*) /* no hint */ FROM t2
  WHERE NOT EXISTS
   (SELECT * FROM t3
    WHERE ROW(5*t2.s1,77)=
     (SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
      (SELECT * FROM t5) AS t5)));
```

 A subquery can return a scalar (a single value), a single row, a single column, or a table (one or more rows of one or more columns). These are called scalar, column, row, and table subqueries. Subqueries that return a particular kind of result often can be used only in certain contexts, as described in the following sections.

 There are few restrictions on the type of statements in which subqueries can be used. A subquery can contain many of the keywords or clauses that an ordinary [`SELECT`](select.html) can contain: `DISTINCT`, `GROUP BY`, `ORDER BY`, `LIMIT`, joins, index hints, [`UNION`](union.html) constructs, comments, functions, and so on.

 Beginning with MySQL 8.0.19, [`TABLE`](table.html) and [`VALUES`](values.html) statements can be used in subqueries. Subqueries using [`VALUES`](values.html) are generally more verbose versions of subqueries that can be rewritten more compactly using set notation, or with [`SELECT`](select.html) or [`TABLE`](table.html) syntax; assuming that table `ts` is created using the statement [`CREATE TABLE ts VALUES ROW(2), ROW(4), ROW(6)`](create-table-select.html), the statements shown here are all equivalent:

```
SELECT * FROM tt
    WHERE b > ANY (VALUES ROW(2), ROW(4), ROW(6));

SELECT * FROM tt
    WHERE b > ANY (2, 4, 6);

SELECT * FROM tt
    WHERE b > ANY (SELECT * FROM ts);

SELECT * FROM tt
    WHERE b > ANY (TABLE ts);
```

 Examples of [`TABLE`](table.html) subqueries are shown in the sections that follow.

 A subquery's outer statement can be any one of: [`SELECT`](select.html), [`INSERT`](insert.html), [`UPDATE`](update.html), [`DELETE`](delete.html), [`SET`](set-variable.html), or [`DO`](do.html).

 For information about how the optimizer handles subqueries, see [Section 8.2.2, “Optimizing Subqueries, Derived Tables, View References, and Common Table Expressions”](subquery-optimization.html). For a discussion of restrictions on subquery use, including performance issues for certain forms of subquery syntax, see [Section 13.2.11.12, “Restrictions on Subqueries”](subquery-restrictions.html).

