Stop 07 · Subqueries, CTEs & Set OperationsTopics · 3/9

Correlated Subqueries

Part of the SQL Roadmap

In short

"Show each customer's most recent order" or "orders above that customer's own average." The comparison changes for every row, so the subquery has to look at the outer row. That's a correlated subquery.

A correlated subquery refers to the outer query's row, so it is conceptually run once per row. It is powerful but can be slow on large tables.

The jaadu stands at a running track finish line with a stopwatch, flipping a thick record book to a different page as each young runner crosses the line.

Think of it like this

The coach and the age-group record book

At a district athletics meet in Kerala, the coach checks each runner's time against the best time in their own age group. For every runner, she flips the record book to that runner's age-group page. The book is the same, but the page changes with each runner. A correlated subquery re-reads the inner query for each outer row, using that row's values.

Watch it happen

The inner query runs once per outer row

outer row
inner query
result

customer 7?

1/5The outer query reads an order from customer 7 and passes 7 into the subquery.

The big ideas

What you need to know

It refers outward

The inner WHERE uses a column from the outer query, like o2.customer_id = o.customer_id.

Once per row, in theory

Logically it runs for every outer row. Optimisers often rewrite it, but on big tables it can still be slow.

Aliases are essential

The same table appears inside and outside, so o and o2 tell them apart.

Index the link

An index on (customer_id, order_date) makes each inner lookup fast.

See it in code

Walk through the code

sql
1SELECT o.customer_id, o.order_id, o.order_date, o.amount2FROM orders AS o3WHERE o.order_date = (4  SELECT MAX(o2.order_date)5  FROM orders AS o26  WHERE o2.customer_id = o.customer_id7)8ORDER BY o.order_date DESC;

1/3The outer query walks through every order as o.

Each customer's most recent order. If a customer placed two orders on their latest day, both appear. Works the same in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

WHERE customer_id = customer_id inside the subquery.

Do this instead

Both sides refer to the inner table, so it's always true. Use aliases: o2.customer_id = o.customer_id.

Oops

Using a correlated subquery in SELECT on millions of rows without an index.

Do this instead

Add an index on the linking column, or rewrite with a join or a window function (Phase 8).

Oops

Assuming 'latest order' always returns one row per customer.

Do this instead

Ties on the same date return several. Decide whether that's fine or add a tie-breaker.

Your mission

Above my own average

~20 min

Quick check

What makes a subquery 'correlated'?

Go deeper

  1. 1Find each customer's most recent order with a correlated subquery.
  2. 2Rewrite the same logic with a window function later in Phase 7.
  3. 3Compare run time on a larger table to see the cost.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge