Subqueries
Use a query inside another query
✅ What You Will Learn
A subquery is a SELECT query written inside another query. The inner query runs first and its result is used by the outer query.
Subqueries let you use the result of one query as a filter, a value, or even a table in another query. They are essential for answering questions like "which customers spent more than the average?" or "which products sold more than last month?"
📋 The orders table used in examples
| order_id | customer_name | product | amount | city |
|---|---|---|---|---|
| 1001 | Rahul Sharma | Laptop | 45000 | Delhi |
| 1002 | Priya Verma | Mobile Phone | 18000 | Noida |
| 1003 | Amit Kumar | Headphones | 3500 | Gurgaon |
| 1004 | Sneha Kapoor | Laptop | 52000 | Delhi |
| 1005 | Vikram Singh | Tablet | 28000 | Noida |
Syntax
Examples
📌 Key Points to Remember
- ✓The inner query (subquery) always runs first
- ✓Subqueries in WHERE can return a single value or a list
- ✓Use IN when the subquery returns multiple values
- ✓Use = when the subquery returns exactly one value
- ✓Subqueries in FROM must be given an alias
🏢 Real-World Application
Subqueries solve problems that a single query cannot. "Find all orders above the average order value" — you need the average first (inner query), then compare each order to it (outer query). "Find products that have never been returned" — EXISTS subquery. "Find customers who spent more than the top 10% threshold" — subquery to calculate the threshold, then outer query to filter. Subqueries are used heavily in financial analytics, fraud detection, and any situation where you need to compare each row against a dynamically computed value.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What is a subquery in SQL?
A subquery is a SELECT query nested inside another query. The inner query runs first and its result is used by the outer query. Subqueries can appear in WHERE, SELECT, FROM, and HAVING clauses.
What is the difference between a subquery and a JOIN?
Both combine data from multiple sources. JOINs are generally faster and more readable for combining tables horizontally. Subqueries are useful when you need a calculated single value, or when the inner result is used as a filter.
What is a correlated subquery?
A correlated subquery references a column from the outer query. It runs once for every row in the outer query, making it potentially slow. Example: finding each employee's salary compared to their department's average.
What is the difference between IN and EXISTS in SQL subqueries?
IN checks if a value matches any value in the subquery result list. EXISTS checks if the subquery returns any rows at all (returns true/false). EXISTS is often faster when the subquery returns large result sets.
Can a subquery return multiple columns?
A subquery in WHERE must return a single column (for IN/= comparisons) or a single value (for scalar comparisons). A subquery in FROM (derived table) can return multiple columns.
✏️ Practice Exercise
Write a query to find all products whose total revenue is above the average product revenue across all products.
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.