Single-Row & Multi-Row Subqueries
Single-Row & Multi-Row Subqueries: IN, ANY, and ALL Operators
A Subquery (also called an Inner Query or Nested Query) is a SELECT query embedded inside another SQL statement. Subqueries allow you to compute dynamic intermediate values on the fly without running multiple separate queries from your application code.
Depending on the cardinality of results returned by the inner query, subqueries are classified into Single-Row Subqueries and Multi-Row Subqueries.
1. Single-Row Subqueries
A Single-Row Subquery returns at most one column and one row (a single scalar value). Because it produces a single value, it can be used with standard scalar comparison operators (=, >, <, >=, <=, <>):
Execution Flow:
- 1The inner query (
SELECT AVG(salary) FROM employees) executes once, returning, for example,62000.00. - 2The outer query substitutes this value:
WHERE salary > 62000.00.
= returns more than one row, MySQL halts with error: Error 1242: Subquery returns more than 1 row!2. Multi-Row Subqueries: IN, ANY, and ALL
When an inner query can return multiple rows, scalar operators (=, >) cannot be used directly. You must use multi-row operators: IN, ANY, or ALL.
3. The IN Operator with Subqueries
4. The ANY (or SOME) Operator
The ANY operator compares a scalar value to each value returned by the subquery and evaluates to TRUE if at least one comparison succeeds:
> ANY (subquery): Greater than the minimum value of the subquery.< ANY (subquery): Less than the maximum value of the subquery.= ANY (subquery): Exactly identical toIN!
5. The ALL Operator
The ALL operator compares a scalar value to all values returned by the subquery and evaluates to TRUE only if every single comparison succeeds:
> ALL (subquery): Greater than the maximum value of the subquery.< ALL (subquery): Less than the minimum value of the subquery.<> ALL (subquery): Exactly identical toNOT IN!
6. Best Practices & Common Pitfalls
- Avoid Deeply Nested Subqueries: While SQL supports nesting subqueries 5+ levels deep, deeply nested queries become impossible to read and debug. Use Common Table Expressions (CTEs) or JOINs instead.
- Semi-Join Optimization: In modern MySQL 8.0, the optimizer frequently rewrites
WHERE id IN (SELECT id ...)subqueries into internal Semi-Joins, executing them with the same speed as anINNER JOIN.
Multiple Choice Questions
1. What error occurs if a subquery evaluated against a scalar equality operator (WHERE salary = (...)) returns 3 rows?
A. Error 1064 (Syntax error) B. Error 1242 (Subquery returns more than 1 row) C. It averages the 3 values automatically D. It picks the first row randomly Answer: B Explanation: Scalar comparison operators like = expect exactly one single scalar value; returning multiple rows triggers Error 1242.
2. What is = ANY (subquery) completely identical to in SQL?
A. = ALL B. IN C. NOT IN D. EXISTS Answer: B Explanation: = ANY tests whether the left-hand value matches any element in the subquery result set, which is the exact definition of IN.
3. If a subquery returns values [40000, 60000, 80000], what does the condition WHERE salary > ALL (subquery) require?
A. Salary must be greater than 40000 B. Salary must be greater than 60000 C. Salary must be strictly greater than 80000 (the maximum value) D. Salary must be equal to 80000 Answer: C Explanation: > ALL requires that the value exceed every element in the set, which means it must be strictly greater than the maximum value (80,000).
4. What does > ANY (subquery) evaluate to when the subquery returns [10, 25, 50]?
A. Greater than 50 B. Greater than 10 (the minimum value in the set) C. Exactly equal to 25 D. Less than 10 Answer: B Explanation: > ANY is satisfied if the value is greater than at least one element, meaning it only needs to exceed the minimum element (10).
5. How does the MySQL query optimizer optimize independent non-correlated subqueries?
A. It executes the inner query once, caches the result set, and probes it against the outer query B. It re-runs the inner query for every row in the outer query C. It deletes the inner query D. It turns the query into a CSV Answer: A Explanation: Non-correlated subqueries do not depend on outer row values; MySQL evaluates them once, caching or materializing the result for outer query evaluation.
Subqueries in SELECT, FROM & WHERE
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Emulating EXCEPT / MINUS in MySQL | Subqueries in SELECT, FROM & WHERE |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.