What are Subqueries in SQL?
Understand SQL subqueries: scalar, multi-row, and derived-table forms, where they go in WHERE, FROM, and SELECT, and when to use them over JOINs.
Expected Interview Answer
A subquery is a query nested inside another SQL statement, whose result is used by the outer query to filter, compute, or compare data.
Subqueries are written in parentheses and can appear in the WHERE, FROM, SELECT, or HAVING clauses. They come in several shapes: scalar subqueries return a single value, row or column subqueries feed operators like IN and ANY, and table subqueries (derived tables) act as a temporary result set the outer query reads from. A non-correlated subquery runs once independently, while a correlated one references the outer query and runs per outer row.
- Breaks complex logic into readable, layered steps
- Lets you filter on aggregated or computed values
- Avoids storing intermediate results in temporary tables
- Supports IN, EXISTS, ANY, and ALL comparisons
- Can be used in SELECT, FROM, WHERE, and HAVING clauses
AI Mentor Explanation
Picture a selector picking batters who scored above the team's average this season. First an assistant computes that average from the full scorecard, then hands the number back so the selector can shortlist anyone above it. The inner calculation of the average is the subquery; the selector's shortlist is the outer query using that single number to make its decision.
Step-by-Step Explanation
Step 1
Identify the inner need
Determine what intermediate value or set the outer query depends on, such as an average or a list of IDs.
Step 2
Write the subquery in parentheses
Compose the inner SELECT that produces that value or set, wrapped in parentheses.
Step 3
Choose the placement
Put the subquery in WHERE, FROM, SELECT, or HAVING depending on how the outer query needs its result.
Step 4
Pick the right operator
Use =, IN, EXISTS, ANY, or ALL to match a scalar, list, or existence test to the outer query.
Step 5
Validate independently
Run the subquery alone first to confirm it returns the expected shape and values before nesting it.
What Interviewer Expects
- Definition of a subquery as a query nested in another statement
- Knowing scalar vs multi-row vs table (derived) subqueries
- Understanding which clauses can contain a subquery
- Difference between correlated and non-correlated subqueries
- When to choose a subquery versus a JOIN
Common Mistakes
- Returning multiple rows to an operator like = that expects a single value
- Confusing a correlated subquery with an independent one
- Forgetting to alias a derived table used in the FROM clause
- Using a subquery where a JOIN would be clearer and faster
- Assuming subqueries always run once, ignoring per-row correlated execution
Best Answer (HR Friendly)
“A subquery is simply a query placed inside another query. The inner query works out something first, like an average or a list, and the outer query uses that result to finish the job, which helps break a complicated question into manageable steps.”
Code Example
-- Scalar subquery in WHERE: employees earning above the average
SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
-- Multi-row subquery with IN: orders from customers in India
SELECT order_id, amount
FROM orders
WHERE customer_id IN (
SELECT customer_id FROM customers WHERE country = 'India'
);
-- Derived table (subquery in FROM): average salary per department
SELECT dept_id, avg_salary
FROM (
SELECT dept_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY dept_id
) AS dept_avg
WHERE avg_salary > 50000;Follow-up Questions
- What is the difference between a correlated and a non-correlated subquery?
- When would you use a subquery instead of a JOIN?
- What is a scalar subquery?
- Why must a derived table have an alias?
- How do IN, EXISTS, ANY, and ALL differ when used with subqueries?
MCQ Practice
1. Which clause can a SQL subquery NOT be placed in?
Subqueries can appear in WHERE, FROM, SELECT, and HAVING clauses, making all listed placements valid.
2. What does a scalar subquery return?
A scalar subquery returns exactly one row and one column, i.e. a single value usable with = or comparison operators.
3. Why must a subquery in the FROM clause be aliased?
A FROM-clause subquery is a derived table and needs an alias so the outer query can reference its columns.
Flash Cards
What is a subquery? — A query nested inside another SQL statement whose result the outer query uses.
What is a scalar subquery? — A subquery that returns a single value (one row, one column), usable with = or comparison operators.
Where can subqueries appear? — In WHERE, FROM, SELECT, and HAVING clauses.
What is a derived table? — A subquery in the FROM clause that acts as a temporary table and must be given an alias.