r/SQL • u/ShipApprehensive4253 • Jun 15 '26
MySQL SQL Correlated Subqueries
Hey everyone, I’m about a week into learning SQL (doing a Data Analyst track) and I’ve officially hit my first major wall at data manipulation.
Regular subqueries make sense, but correlated subqueries are completely tripping me up.
Could anyone explain:
- How they actually work under the hood?
- Why they are different from non-correlated ones?
- What we actually use them for, and are they basically just another version of a self-join?
Any simple analogies or step-by-step breakdowns would be massively appreciated. Thanks!
1
Upvotes
6
u/Ginger-Dumpling Jun 15 '26
Copied query from https://en.wikipedia.org/wiki/Correlated_subquery
Think of it like a loop. For every row in the outer query, do something in the sub-query that is dependent on value(s) from the outer query...like which department they're in. You'll see them in SELECT/WHERE portions.
This can be expressed as a non-correlated subquery too.
Whether the query planner handles them the same way and whether there will be a performance difference probably vaires from DB to DB and version to version.
3
u/Eleventhousand Jun 15 '26
Keep in mind that they often perform more poorly than choose alternate paths. Keep in mind that SQL almost always involves writing something, and then revising for more performance or accuracy.
A correlated subquery is a query in which there is a subquery that references the main query. For example, in the query below, a subquery looks at all of the sales data and sums that up, but it does it for each match to cust_id in the main query. As you can see, its not used a self-join in this case. In my example here, it's used more as a shortcut in the cases of "I'm too lazy t the moment to join two tables together and then group by everything, so I'll just plop this in here."
1
u/DavidGJohnston Jun 15 '26
An “exists” expression is probably the easiest to keep in mind when thinking about a correlated subquery. A where exists B. Usually the rows in B you are looking for are related to A in some fashion. Hence the B subquery is correlated to A. Mechanically it’s easiest to think, for each row in A execute the subquery using the values from the columns in A. A normal subquery often just gets executed once and cached for later scanning or use; a correlated subquery, because of the inputs, usually it actually executed multiple times. Though ideally in many cases you can write a correlated or uncorrelated subquery and let the planner determine how best to actually execute the result. This is how joins works generally. Though another common usage for a correlated subquery is A lateral join B, where B is “select … where b.col=a.col limit 1”.
1
u/Happy-Personality-15 Jun 15 '26
Ignore them for now. Focus on the basics of joins , cte. Sub queries don’t get tested much or used much in practice
1
u/DaOgDuneamouse Jun 17 '26
I'm honestly not a fan of correlated subqueries. You have to reference fields in the outer query, within the inner one. This forces the inner query to be evaluated once for each line in the outer one, and that just feels needlessly inefficient. I usually tend to use a series of WITH clause CTEs. The top CTE gets my base population and every-thing else references that done, it's kind of a daisy chain. It keeps things much cleaner and you don't get tangled in field scope.