Key Ideas
1What Is a Subquery?. A subquery (also called an inner query or nested query) is a complete SELECT statement enclosed in parentheses and used inside another SQL statement. The outer query r...
2Subquery in WHERE. The most common use — filter rows using a value or set produced by the inner query.
3Subquery in FROM — Derived Table. When you put a subquery in the FROM clause it becomes a derived table — a temporary result set you can query just like a real table. You must give it an alias.
4Correlated Subquery. A correlated subquery references a column from the outer query. It executes once per row of the outer query — making it powerful but potentially slower on large datasets.
5EXISTS vs IN. Both EXISTS and IN check whether rows satisfy a condition, but they work differently: