Subqueries — correlated vs uncorrelated
WHY do subqueries exist?
WHAT exactly is each kind?
HOW to tell them apart (the test)

Worked Example 1 — Uncorrelated (scalar)
Worked Example 2 — Uncorrelated (list with IN)
Worked Example 3 — Correlated (EXISTS)
Worked Example 4 — Correlated (per-row aggregate)
Recall Feynman: explain to a 12-year-old
Imagine a worksheet. Uncorrelated: the teacher writes one number on the board (say, the class average), and you compare your score to that same number — the board is written once. Correlated: every student must look up their own group's average from a different table each time — so the lookup happens again for each student. Same idea, but one is a shared answer and the other is a personal answer per row.
Flashcards
What single test distinguishes a correlated from an uncorrelated subquery?
How many times is an uncorrelated subquery logically evaluated?
How many times is a correlated subquery logically evaluated?
Why can't you write WHERE salary > AVG(salary) directly without a subquery?
Why is EXISTS often preferred over IN for correlated existence checks?
Steel-man: are correlated subqueries always slow?
Give the naive cost of a correlated subquery with N outer rows and inner cost c.
"Find employees above their own department's average" — correlated or uncorrelated, and why?
Connections
- Joins — inner outer cross — correlated subqueries can often be rewritten as joins.
- Aggregate Functions GROUP BY — subqueries are how you compare rows against aggregates.
- EXISTS vs IN vs ANY — choosing the right correlation operator.
- Query Optimizer Execution Plans — how the DB decides whether to loop or rewrite.
- NULL handling in SQL — why IN can misbehave where EXISTS is safe.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, subquery ka matlab hai ek query ke andar dusri query. Sabse bada sawaal yeh hai: kya andar wali (inner) query bahar wali (outer) query ke current row par depend karti hai? Agar nahi karti, to woh uncorrelated hai — usko sirf ek baar chalao, answer nikaalo, aur use kar lo. Jaise "company ka average salary" ek hi number hai, to use ek baar compute karke compare kar lo.
Agar inner query outer ka koi column use karti hai (jaise e.dept_id), to woh correlated hai — ab har outer row ke liye inner query dobara-dobara chalegi, kyunki har row ka apna dept alag hai, alag average. Soch lo jaise loop chal raha ho jo har baar alag argument ke saath function call karta hai.
Test simple hai: inner query ko alag se copy karke chalao. Agar woh akele chal jaaye → uncorrelated. Agar "unknown column" error de kyunki use outer ka column chahiye → correlated. EXISTS waale correlated checks aksar fast hote hain kyunki pehla match milte hi ruk jaate hain.
Ek galatfehmi: log sochte hain correlated hamesha slow hota hai. Yeh sirf logical model hai — asli mein database ka optimizer ise join/semi-join mein badal deta hai, to speed barabar ho sakti hai. Isliye assume mat karo, execution plan padho. Yeh concept interviews aur real reporting queries (jaise "har department ke top earners") mein bahut kaam aata hai.