How to interpret correlated subqueries? EDIT:
Both answers are great but I think I must clarify my question.

NOTE: I must indicate that in the following, by executed, I mean logically. I understand that the actual execution made by the particular DBMS may use a completely different approach. So what I will ask is not about the actual execution but rather about the evaluation logic of a correlated subquery.

What I particularly want to know is:

"Why a correlated subquery is executed in this way?" For example, for the example query I have given, why isn't it executed in the following way:


  "Return the rows of the cities_stores relation whose store_type is
  equal to some row's store_type in stores relation. If the
  resulting relation has at least 1 element, the value of EXISTS
  clause is TRUE. Otherwise, its value is FALSE."


but rather in this way:


  "For each row processed in the outer query (let's call this row as
  "current row"), process the inner (correlated) subquery, using the
  current row's values for the attributes not present in the subquery's
  FROM clause."


and from where can I learn to think about correlated subqueries in the latter method, instead of the former one?

I hope this is not an unwise question to ask but exactly this is what I don't understand.



Correlated subqueries are highly confusing me.

Example query from here:

SELECT DISTINCT store_type FROM stores
  WHERE EXISTS (SELECT * FROM cities_stores
                WHERE cities_stores.store_type = stores.store_type);


Initially, I thought that EXISTS clause would have a value that doesn't change for each row of the outer query and that value would be calculated in the following way:

"Return the rows of the cities_stores relation whose store_type is equal to some row's store_type in stores relation. If the resulting relation has at least 1 element, the value of EXISTS clause is TRUE. Otherwise, its value is FALSE."

Had the EXISTS statement evaluated in this way, it would have a value that does not change among various rows for the outer relation and hence, the query would be equivalent to the following:

SELECT DISTINCT store_type FROM stores
  WHERE TRUE; -- Or FALSE, if the subquery did not return any rows.


And of course this made me think how is an EXISTS statement is useful at all?

But apparently, that isn't the way a correlated subquery is executed. Apparently, a correlated subquery is executed in the following way: 

"For each row processed in the outer query (let's call this row as "current row"), process the inner (correlated) subquery, using the current row's values for the attributes not present in the subquery's FROM clause."

So finally, what I don't understand:


Why is this the way it happens? I mean, why not it doesn't happen in the way I initially thought? (That is: Why it doesn't return the rows of the cities_stores relation whose store_type is equal to some row's store_type in stores relation?) From exactly where am I supposed to interpret that it is how SQL works?
In various sources around the internet, I constantly see the following about the correlated subqueries:


  In a correlated subquery, the subquery is evaluated once for each row
  processed by the outer query.


But most curiously, a statement similar to this does not exist in MySQL's page about correlated subqueries. So:

Is that an incompleteness in MySQL manual, or is such a statement simply not required? If it is not required, from where am I supposed to understand that it the way how a correlated subquery is evaluated? Or, as another possibility, is the quote I have given simply a vague statement that is not completely correct? If so, how am I supposed to interpret a correlated subquery?
This specific case (the exists predicate) is a bit of an outlier as far as correlated subqueries go.  ish...

Some correlated subqueries are certainly evaluated once for every row in the outer query, but I don't think you can apply that across the board, and I specifically think that's a statement that cannot be applied with authority to the exists predicate.

The SQL example below this paragraph is highly contrived, but nevertheless illustrates an "evaluated once for each row in the outer query" correlated subquery. It's "highly contrived" because a straight relational join would be a much better solution, and in this simple case the join is super easy to write.

select
    team_name
    , (
       select league_name
       from leagues
       where leagues.league_id = teams.league_id
    ) as league_name
from teams


In this instance, each row in the result set is going to contain the name of the league that the team belongs to, because of the correlated subquery. And the correlated subquery is probably going to be run once for each row in the outer query. Maybe.

The thing is, the database's query optimizer might rewrite this as a more efficient join, depending on the database. So even this simple example might not be executed "once per row in the outer query."

Either way, the subquery is dependent on the values returned from the table in the outer query, and that makes it a correlated subquery--because of the correlation with the outer table.

Part of the point of SQL in general is that the specific implementation is not important to you.

You should think in terms of sets of data (think set arithmetic), not in terms of iterations and comparisons per iteration.

The exists predicate doesn't make you any promises about the particular implementation. In general, exists is supposed to take advantage of table statistics and indexes in such a way that it produces a more efficient query plan than many other approaches will (and more efficient than you're likely to come up with on your own).

The actual query that ends up being run is going to be some kind of join, or multiple joins. But you don't need to get worked up about that. That's why your exists clause contains select * instead of identifying specific fields.

The query plan produced by the database engine will figure out which fields it wants to use, which key or keys and which index or indexes matter, etc.

So your exists example is still a correlated subquery, despite the underlying implementation, because the inner query refers to and is logically dependent upon the table in the outer query.

SELECT DISTINCT store_type FROM stores
WHERE EXISTS
    (
    SELECT * FROM cities_stores
    WHERE cities_stores.store_type = stores.store_type
    );