SQL SUBQUERY Note 1
 SQL SUBQUERY Note 1

SQL SUBQUERY Note 1

Status
Done

🔍 What is a Subquery?

subquery (also called an inner query or nested query) is a query placed inside another SQL query — used to return data that the outer query depends on.

It is enclosed within:

  • () parentheses
  • Often used in WHEREFROM, or SELECT clauses

🔍 Why Use Subqueries?

  • To break complex problems into smaller pieces
  • To avoid joins in some cases
  • To filter, transform, or compare data using values from another query

🔍 Types of Subqueries?

  • Independent (Non-correlated) Subquery
  • Correlated Subquery

🔎 How to Identify Each?

Independent Subquery

  • It can run alone.
  • It does not refer to any column from the outer query.

💡 Key sign: No column inside the subquery refers to the outer query’s table.

Correlated Subquery

  • It depends on values from the outer query.
  • It must reference a column from the outer query.
  • It cannot run on its own.

💡 Key sign: You’ll see a column from the outer query inside the subquery.

Independent Subqueries

Correlated Subqueries