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 ...

Get SQL: Visual QuickStart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.