Chapter 10: Subqueries & Nested Queries

A subquery (also called a nested query) is simply a query inside another query. It’s like asking a small question to get an answer, and then using that answer to ask the big question.

Subqueries can appear in three main places:

  • In SELECT (as a calculated column)
  • In FROM (as a temporary table)
  • In WHERE (most common – to filter rows)

We’ll also learn correlated subqueries (the subquery depends on the outer query), EXISTS / NOT EXISTS, and ANY / ALL.

First – let’s make sure our data is ready We’ll use the same students, courses, and enrollments tables from Chapter 9. If you haven’t run the setup yet, quickly do this:

SQL

Now let’s dive in!

1. Subqueries in WHERE Clause (Most Common)

Syntax:

SQL

Example 1 – Students who enrolled in Mathematics

SQL

Step-by-step what happens:

  1. Innermost: Finds course_id of ‘Mathematics’ → say 1
  2. Middle: Finds all student_ids enrolled in course_id 1
  3. Outer: Shows only those students

Example 2 – Students who paid full fees for at least one course

SQL

2. Subqueries in SELECT Clause (Calculated Column)

Syntax:

SQL

Example – Show each student’s total courses enrolled

SQL

Result example:

text

3. Subqueries in FROM Clause (Derived Table / Temporary Table)

Syntax:

SQL

Example – Find courses with more than 1 student enrolled

SQL

This is like creating a temporary table called course_stats and then filtering it.

4. Correlated Subqueries

(The subquery depends on the outer query – runs once per outer row)

Example – Students who enrolled in more than 1 course

SQL

Result: Only Priya Sharma (because she enrolled in 2 courses)

Notice: The subquery uses s.student_id from the outer query → that’s what makes it correlated.

5. EXISTS / NOT EXISTS

(Checks if subquery returns at least one row – very efficient!)

Syntax:

SQL

Example – Students who are enrolled in at least one course

SQL

Opposite – Students who have NOT enrolled in any course

SQL

Result: Rohan Mehta, Meera Desai, Arjun Reddy (and any others with no enrollments)

Why use EXISTS instead of IN?

  • EXISTS stops as soon as it finds one match → faster for large tables
  • Works perfectly with correlated subqueries

6. ANY / ALL

(Compares a value with any or all values returned by subquery)

Operator Meaning Example Meaning
ANY At least one value matches Greater than ANY → greater than at least one
ALL Matches all values Greater than ALL → greater than every one

Example – Students who enrolled in a course that costs more than ANY course fees

SQL

Better example – Students enrolled in the most expensive course

SQL

Using ALL – Courses more expensive than ALL other courses (i.e., the most expensive)

SQL

That’s it for Chapter 10! 🎉 You can now write very advanced, nested queries that answer complex questions!

Homework for today (do it right now – very important!) Run these 5 queries and paste the output of each (at least first few rows):

SQL

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *