4.4.7 · D1Databases

Foundations — Subqueries — correlated vs uncorrelated

1,920 words9 min readBack to topic

Before you can safely read the parent topic (index 4.4.7), you must be able to read every symbol it throws at you without stopping to guess. This page builds each one from nothing — plain words, then a picture, then why the topic needs it.


0. The absolute starting point: a table

Everything in databases sits on top of one picture: a table — a grid of rows and columns, like a spreadsheet.

Figure — Subqueries — correlated vs uncorrelated

1. SELECT ... FROM ... WHERE — the shape of every query

A query is a sentence that reads rows out of a table.


2. The subquery: a query in brackets

Figure — Subqueries — correlated vs uncorrelated

3. What a subquery can return — scalar, list, table

A subquery's answer comes in one of three shapes. You must recognise each because different operators expect different shapes.

Figure — Subqueries — correlated vs uncorrelated

4. Aliases — giving a table a nickname (e, o, c)

This is the single symbol most beginners trip on when reading correlated subqueries.


5. AVG(...) and aggregation — collapsing many rows into one


6. IN, EXISTS, SELECT 1 — the membership operators


7. The cost symbols — , ,

The topic's cost formula uses three symbols. Here is each, plainly.


The prerequisite map

Table rows and columns

SELECT FROM WHERE query

Reading order FROM then WHERE then SELECT

Subquery a query in brackets

Return shapes scalar list table

Table aliases e and o and c

Aggregates like AVG

IN and EXISTS operators

Cost symbols N and c and k

Correlated vs Uncorrelated


Equipment checklist

Point to a row versus a column in a table
A row is one horizontal record (one employee); a column is one vertical field (all salaries).
The order the database actually processes a query
FROM (pick grid) → WHERE (filter rows) → SELECT (choose columns).
Why WHERE cannot use AVG(salary) directly
WHERE runs one row at a time, before any aggregate over all rows has been computed.
What a subquery is, in one phrase
A complete SELECT inside parentheses whose answer the outer query uses.
The three shapes a subquery can return
Scalar (one cell), list (one column), table (used by EXISTS).
Which operator goes with which shape
scalar↔ > < = , list↔ IN , table↔ EXISTS.
What a table alias like e does
Gives a table a nickname so e.salary names that specific copy's column.
The 5-second correlated test
Does the inner query reference an outer alias/column? Yes → correlated; No → uncorrelated.
What SELECT 1 inside EXISTS means
A throwaway value — EXISTS only checks whether any row came back, not what it holds.
What N, c, and k stand for in the cost model
N = outer rows, c = cost of one inner-query run, k = cost of one cheap comparison.
Why naive correlated cost is N·c not c
The inner query re-runs once for every outer row instead of a single time.

Connections

  • Parent topic (Hinglish) — where these foundations get used.
  • Aggregate Functions GROUP BY — the source of AVG/COUNT that subqueries compare against.
  • EXISTS vs IN vs ANY — choosing the right membership operator.
  • NULL handling in SQL — why IN can misbehave where EXISTS is safe.
  • Joins — inner outer cross — what optimizers rewrite correlated subqueries into.
  • Query Optimizer Execution Plans — how the DB decides loop vs rewrite.