A SELECT statement nested inside another query, used in WHERE, FROM, SELECT, or HAVING clauses.
A query nested inside another query, useful for filtering, computed columns, or derived tables. Correlated subqueries (that reference outer rows) can be slow; always check if a JOIN or window function can replace them for better performance.
Compatibility
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | Native | all | All subquery types supported. Optimization historically weak in older versions — greatly improved in 8.0 with hash joins and derived condition pushdown. |
| PostgreSQL | Native | all | Excellent support for all subquery types. Also supports LATERAL subqueries and efficient EXISTS/NOT EXISTS. |
| SQL Server | Native | all | Full support for scalar, correlated, and derived-table subqueries. |
| Oracle | Native | all | Full support. Scalar subqueries can appear in SELECT lists and many other expression contexts. |
| SQLite | Native | all | Full support for correlated subqueries, EXISTS, and derived tables. |
Details
Subqueries can often be rewritten as JOINs or CTEs for readability or performance. MySQL pre-8.0 had known optimizer issues with IN subqueries that were fixed in 8.0.
Standard Syntax
Version Support
Per-Database Syntax & Notes
MySQL Native syntax
All subquery types supported. Optimization historically weak in older versions — greatly improved in 8.0 with hash joins and derived condition pushdown.
PostgreSQL Native syntax
Excellent support for all subquery types. Also supports LATERAL subqueries and efficient EXISTS/NOT EXISTS.
SQL Server Native syntax
Full support for scalar, correlated, and derived-table subqueries.
Oracle Native syntax
Full support. Scalar subqueries can appear in SELECT lists and many other expression contexts.
SQLite Native syntax
Full support for correlated subqueries, EXISTS, and derived tables.