SQL Subqueries Explained with Real Interview Examples | SQL Interview Series #2
If SQL Joins are one of the most frequently asked interview topics, Subqueries are not far behind.
During SQL interviews, recruiters often test your ability to retrieve data using nested queries rather than relying solely on joins. Understanding subqueries can help you solve complex problems in a clean and efficient way.
In this article, we’ll learn what subqueries are, explore different types of subqueries, and solve real interview-style examples.
What is a Subquery?
A subquery is a query written inside another SQL query.
The inner query executes first, and its result is used by the outer query.
Basic Syntax
SELECT column_name
FROM table_name
WHERE column_name =
(
SELECT column_name
FROM another_table
);Think of a subquery as a query that helps another query make a decision.
Example 1: Find Employees Earning Above Average Salary
Employees Table
Query
SELECT EmployeeName, Salary
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);Explanation
First, the subquery calculates the average salary.
SELECT AVG(Salary) FROM Employees;Average Salary = 55000
Then the outer query returns employees earning more than 55,000.
Result
Types of Subqueries
1. Single-Row Subquery
Returns only one value.
Example:
SELECT *
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);The subquery returns a single value.
2. Multiple-Row Subquery
Returns multiple rows.
Example:
SELECT EmployeeName
FROM Employees
WHERE DepartmentID IN
(
SELECT DepartmentID
FROM Departments
WHERE Location = 'Mumbai'
);The IN operator is commonly used with multiple-row subqueries.
Subquery with IN Operator
Suppose we have two tables:
Customers
Orders
Find customers who have placed orders.
SELECT CustomerName
FROM Customers
WHERE CustomerID IN
(
SELECT CustomerID
FROM Orders
);Result
Varsha
Get Varsha Vishwakarma’s stories in your inbox
Join Medium for free to get updates from this writer.
Rahul
Correlated Subquery
A correlated subquery depends on the outer query.
Unlike normal subqueries, it executes once for every row processed by the outer query.
Example
Find employees earning more than the average salary of their department.
SELECT e.EmployeeName,
e.Salary
FROM Employees e
WHERE e.Salary >
(
SELECT AVG(Salary)
FROM Employees
WHERE DepartmentID = e.DepartmentID
);This is one of the most popular SQL interview questions.
Subquery vs JOIN
A common interview question is:
“Should I use a JOIN or a Subquery?”
The answer depends on the problem.
Use a JOIN when:
- Combining data from multiple tables
- Retrieving related information
Use a Subquery when:
- You need an intermediate result
- The logic becomes easier to understand
In many cases, both approaches can produce the same result.
Frequently Asked Interview Questions
1. What is a Subquery?
A query nested inside another SQL query.
2. Can a Subquery return multiple rows?
Yes. Operators such as IN, ANY, and EXISTS are commonly used with multiple-row subqueries.
3. What is a Correlated Subquery?
A subquery that references columns from the outer query.
4. Which is faster: JOIN or Subquery?
There is no universal answer. Performance depends on the query structure, indexing, and database engine.
Real-World Example
Imagine you’re building a food delivery platform.
You need to find customers who have placed more orders than the average customer.
A subquery can first calculate the average number of orders and then compare each customer’s order count against that value.
This type of business logic appears frequently in analytics and reporting systems.
Final Thoughts
Subqueries are one of the most powerful tools in SQL. They help break complex problems into smaller, manageable steps and are frequently asked in technical interviews.
If you’re preparing for SQL interviews, make sure you understand:
- Single-row subqueries
- Multiple-row subqueries
- IN operator
- Correlated subqueries
- JOIN vs Subquery scenarios
Mastering these concepts will make solving advanced SQL problems much easier.
In the next article of this SQL Interview Series, we’ll explore Correlated vs Non-Correlated Subqueries with practical examples and interview questions.
Happy Learning! ✨








