There are two types of subqueries, correlated and non-correlated. The following table outlines their differences:
Non-correlated |
Correlated |
The inner query doesn't depend on the outer query |
Inner query depends on the outer query |
Can run as a standalone query |
Can't run as a standalone query |
Executed only once |
Executed once for each row selected in the outer query |
Executed before the outer query |
Executed after the outer query |
Can't be used instead of JOIN on the outer query |
Can be used instead of JOIN on the outer query, but will be slower than JOIN |
The following sections will show examples of each to help you understand them better.