4.4.7Databases

Subqueries — correlated vs uncorrelated

1,691 words8 min readdifficulty · medium

WHY do subqueries exist?


WHAT exactly is each kind?


HOW to tell them apart (the test)

Figure — Subqueries — correlated vs uncorrelated

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?
Whether the inner query references a column/alias from the outer query. If yes → correlated; if no → uncorrelated.
How many times is an uncorrelated subquery logically evaluated?
Once, before the outer query uses its result.
How many times is a correlated subquery logically evaluated?
Once per row of the outer query (semantically — optimizers may rewrite it).
Why can't you write WHERE salary > AVG(salary) directly without a subquery?
WHERE is evaluated per-row before aggregation; you need the aggregate computed first, which the (uncorrelated) subquery provides.
Why is EXISTS often preferred over IN for correlated existence checks?
EXISTS short-circuits at the first match and behaves predictably with NULLs, whereas IN with NULLs can yield surprising/empty results.
Steel-man: are correlated subqueries always slow?
No — that is only the logical semantics; optimizers frequently rewrite them into joins/semi-joins, so performance can equal an uncorrelated query.
Give the naive cost of a correlated subquery with N outer rows and inner cost c.
~N·c (plus per-row comparisons), versus ~c for uncorrelated.
"Find employees above their own department's average" — correlated or uncorrelated, and why?
Correlated — the threshold depends on each row's dept_id, so the inner aggregate must be recomputed per row.

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

motivates

split into

split into

no ref

yes ref

evaluated

evaluated

example

example

feels like

corrected by

Subquery: query inside query

Need answer depending on another answer

Uncorrelated subquery

Correlated subquery

Diagnostic: mentions outer column?

Runs once, plug in value

Re-runs per outer row

Ex: salary > AVG salary

Ex: id IN order list

Myth: correlated always slow

Optimizer rewrites to joins

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.

Go deeper — visual, from zero

Test yourself — Databases

Connections