[Solved] How subquery works?


The query in the parenthesis is called a correlated sub-query.

That is because, if you look at the first FROM, it has an alias for the salary table FROM salary a, which in this case is a.

The sub-query references the outer query in the WHERE b.salary > a.salary condition, where it checks the condition for each row in the nested sub-query.

This is how it works in large, as you might now, but the actual execution of the query, behind the scenes, might differ depending on data, available resources and many other factors.

SQL is a declarative language which means that you just tell it what you want to do with the data, through your query, and it tries to finds out what is the best way (performance-wise) to return the data.

This can be done in the form of nested loops, a hash join or an adaptive join in some newer RDBMS’s.

solved How subquery works?