SQL Subqueries | Nested SELECT Statements
# CHAPTER 18
Subqueries and Nested Queries
1. Introduction
Sometimes, to answer a question, you have to ask a different question first. *Example:* "Who earns more than the average salary?" To answer this, you must first ask, "What *is* the average salary?" In SQL, you cannot just writeWHERE salary > AVG(salary)—the engine will crash. To solve this, you must write a query *inside* another query. This is called a Subquery (or Nested Query). In this chapter, we will master the logic of query-inception.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the structural syntax of a Subquery.
-
Use a Subquery inside a
WHEREclause for dynamic filtering.
-
Use a Subquery inside a
SELECTclause to generate calculated columns.
-
Understand the
EXISTSoperator for performance optimization.
-
Differentiate between a Subquery and a
JOIN.
3. The Subquery in the WHERE Clause
The most common place for a subquery is acting as a dynamic filter.
Let's solve the intro problem: Find employees earning above average.
*(The inner query must be wrapped in parentheses ()).*
4. Subqueries returning a List (IN)
If your inner query returns multiple rows, you cannot use the equals = or greater than > operator. You must use the IN operator.
*(Note: This exact same result could be achieved with a JOIN. Developers often debate which is better. Generally, JOIN is faster, but Subqueries are sometimes easier for humans to read).*
5. The Subquery in the SELECT Clause
You can use a subquery to generate a dynamic column on the fly.
*(Result: A beautiful grid comparing every single product's price to the global average!)*
6. The EXISTS Operator (High Performance)
If you just want to know *if* a customer has placed an order (you don't care what the order is), using an INNER JOIN or an IN subquery forces SQL to process massive amounts of data.
The EXISTS operator is highly optimized. It stops searching the exact millisecond it finds a single match, making it blazing fast.
*(This is a Correlated Subquery. The inner query relies on the c.id from the outer query to function!).*
7. Mini Project: The HR Analytics Dashboard
Let's use subqueries to build a complex HR report.8. Common Mistakes
-
Returning Multiple Columns: If you write
WHERE salary = (SELECT id, salary FROM employees), the query will crash. A subquery used with a standard mathematical operator (=,>,<) can ONLY return a single column and a single row (a scalar value).
-
Subquery Performance: Writing a Correlated Subquery inside a
SELECTstatement forces the inner query to re-execute for *every single row* in the outer table. If you have 1 million rows, the inner query runs 1 million times. This is called the "N+1 Problem" and destroys server performance.
9. Best Practices
-
JOIN vs Subquery: As a general rule of database architecture, if you can write a query using a standard
JOIN, you should. The SQL Query Optimizer is incredibly efficient at processingJOINs. Use Subqueries specifically when calculating aggregates (MAX,AVG) or utilizing theEXISTSoperator.
10. Exercises
- 1. What punctuation marks must encapsulate a nested Subquery?
-
2.
If a Subquery in a
WHEREclause returns a list of 50 different IDs, what operator must you use instead of the equals (=) sign?
11. SQL Challenges
Write a query to find allmovies where the budget is greater than the MAX budget of movies released in the year 2010.
12. MCQ Quiz with Answers
When utilizing a Subquery in a WHERE clause combined with a standard comparison operator like >, what strict rule applies to the output of the Subquery?
Why is the EXISTS operator considered a high-performance optimization over the IN operator when checking for relationships?
13. Interview Questions
- Q: Differentiate between a standard Subquery and a "Correlated Subquery". Provide an example of how a Correlated Subquery operates row-by-row against the outer query.
-
Q: You are tasked with finding all Users who have never placed an Order. Explain how you could solve this using a
LEFT JOINcombined with aWHERE NULLclause, versus solving it using aNOT EXISTSsubquery.
14. FAQs
Q: Can I put a Subquery inside theFROM clause?
A: Yes! This is called a "Derived Table". You can do SELECT * FROM (SELECT id, name FROM users) AS temp_table;. It treats the result of the inner query as a temporary, virtual table that you can query against!
15. Summary
You have mastered multi-layered logic. By wielding Subqueries, you can execute complex mathematical filters dynamically, generate calculated columns on the fly, and drastically optimize your backend application's performance using the highly efficientEXISTS operator.