A MySQL subquery is a SELECT statement nested inside another statement. You write it in parentheses, and MySQL uses its result as a value, a list of values or a temporary table in the outer query. Subqueries let you answer questions in steps, such as finding products priced above the average.
This guide covers the places a subquery can appear, the three shapes of result it can return, and how it compares with a join. The queries run on MySQL 8.4 and later.
Two small tables are enough to show every pattern.
The average product price is 106.00, and the Mouse, Keyboard and Lamp are below it.
A scalar subquery returns exactly one row and one column. You can use it anywhere a single value is allowed, most often in a WHERE clause.
MySQL runs the inner query first, gets 106.00, and then filters the outer query with it. If a scalar subquery returns more than one row, MySQL stops with ERROR 1242: Subquery returns more than 1 row. Zero rows produce NULL, which makes the comparison unknown and returns no rows.
When the inner query returns one column and many rows, compare it with IN, NOT IN, ANY or ALL.
This lists the products that have been ordered at least once. Be careful with NOT IN: if the subquery list contains a single NULL, NOT IN returns no rows at all. Make sure the inner column is NOT NULL, filter nulls out, or use NOT EXISTS.
A subquery in the FROM clause is called a derived table. It behaves like a temporary table that exists for the duration of the statement, and MySQL requires you to give it an alias.
Derived tables are useful when you must aggregate first and then filter or join the aggregated result. Since MySQL 8.0, the optimizer can merge simple derived tables into the outer query, so they do not always cost an extra temporary table.
A scalar subquery can also appear in the column list, producing one extra column per outer row.
This subquery refers to p.id from the outer query, which makes it a correlated subquery. That pattern gets its own article, because it behaves differently from the uncorrelated examples above.
Many subqueries can be rewritten as joins, and the best choice depends on what you need.
| Need | Better choice |
|---|---|
| Compare a column with one aggregated value | Scalar subquery |
| Filter rows by membership in another table | IN or EXISTS subquery, or a join |
| Show columns from both tables | JOIN |
| Aggregate first, then filter the aggregate | Derived table |
The same product list from earlier can be written as a join, but a join may return duplicate product rows when a product has several order lines, so you would add DISTINCT:
The subquery version avoids duplicates naturally. Modern MySQL turns many IN subqueries into semijoins internally, so performance is often the same. Use EXPLAIN to check rather than assuming.
FROM needs an alias.UPDATE or DELETE directly. MySQL raises error 1093; wrap the inner query in a derived table to work around it.LIMIT is not allowed inside an IN subquery in some forms, so test it on your server version.A join combines columns from several tables into one row set. A subquery produces a value, list or table that the outer query uses, and it does not by itself add columns to the output.
Yes, in a derived table, or in a row comparison such as WHERE (a, b) IN (SELECT x, y FROM t). A scalar subquery in an expression must return exactly one column.
Not necessarily. The MySQL optimizer rewrites many subqueries into joins or semijoins. Correlated subqueries on large tables can be slow, so check the plan with EXPLAIN.
MySQL does not set a small fixed limit, but deep nesting is hard to read and maintain. Use common table expressions with WITH for long chains.
A subquery is a parenthesized SELECT used inside another statement. It can return a single value, a list for IN, or a derived table in FROM, and it can also appear in the SELECT list. Joins are better when you need columns from both tables, while subqueries are clear for filtering and for aggregate-then-filter questions. Watch for NULL with NOT IN, and for the single-row rule on scalar subqueries.