A subquery is a query nested inside another query. It lets you answer two-step questions in one statement — like 'find employees who earn more than the average salary' — without computing intermediate results by hand.
Key Points
- Scalar subquery returns one value: WHERE salary > (SELECT AVG(salary) FROM employees)
- IN subquery matches a list: WHERE dept_id IN (SELECT id FROM departments WHERE city='Noida')
- EXISTS checks whether matching rows exist — efficient for large sets
- Derived table: a subquery in FROM that acts like a temporary table
- Subqueries in SELECT compute per-row lookups (use carefully)
- Most subqueries can be rewritten as JOINs — know both forms
.png)