Subqueries vs. Joins
In “Understanding Subqueries” earlier in this chapter, Listings 8.3 and 8.4 showed two equivalent queries: one used a join, and the other used a subquery. Many subqueries can be formulated alternatively as joins. In fact, a subquery is a way to relate one table to another without actually doing a join.
As subqueries can be hard to use and debug, you may prefer to use joins, but you can pose some questions only as subqueries. In cases where you can use subqueries and joins interchangeably, the choice is yours. Usually, no performance difference exists between a statement that uses a subquery and a semantically equivalent version that uses a join (but see “Comparing Equivalent Queries” later in this chapter).
The following ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access