From the course: Advanced SQL for Query Tuning and Performance Optimization

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Subqueries vs. joins

Subqueries vs. joins

- Before we move on from our discussion of joins, let's touch on the question of joins vs. subqueries. A subquery can be used to return a value from a related table. In this example, we're essentially performing a lookup on the company_regions table in the part of the select statement where we specify the columns that we want to return from this query. We could rewrite the query to use joins in this example, the results are the same. This is a good example of how we can declaratively specify an outcome in different ways. But this raises a question, so which methods should we use? Now, in the past the conventional wisdom has been that we should always use joins since they'll be more efficient. That may have been true in the past, but optimizers have improved over time and we can build efficient execution plans for subqueries. My advice is to optimize for clarity. So use the method that makes your intentions clear.…

Contents