A subquery is a SELECT statement nested inside another SQL statement to filter, join, or compute derived data.
Subqueries let you keep logic in one statement, reduce network round-trips, and push computation to the database. You can filter, aggregate, or join derived data without temporary tables.
Place a SELECT inside parentheses where a value list is accepted. Common spots are the SELECT list, FROM clause, and WHERE filters.
A correlated subquery references columns from the outer query and runs once per outer row.Use it when each row needs a different derived value.
Use WHERE column IN (SELECT …) to keep rows whose value exists in the subquery result.
Put the subquery in the FROM clause, give it an alias, and join it like a regular table.This is great for pre-aggregated data.
Combine UPDATE with SET column = (SELECT …) or use FROM to join to a subquery that returns the new values.
Select only needed columns, add proper indexes, avoid SELECT *, and favor EXISTS over IN for large result sets to improve performance.
.
Yes, but only when used in the FROM clause or with row-valued comparisons. Single-value contexts like WHERE IN need one column.
Not necessarily. The planner often rewrites subqueries as joins. Performance depends on indexes, row counts, and whether the subquery is correlated.