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 - SQL Tutorial
From the course: Advanced SQL for Query Tuning and Performance Optimization
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
-
-
-
-
-
-
Types of joins2m 36s
-
(Locked)
Nested loops3m 19s
-
(Locked)
Nested loop example plan3m 59s
-
(Locked)
Hash joins1m 29s
-
(Locked)
Hash join example plan1m 54s
-
(Locked)
Merge joins2m 23s
-
(Locked)
Merge join example3m 2s
-
(Locked)
Subqueries vs. joins1m 13s
-
(Locked)
Challenge: Designing a join24s
-
(Locked)
Solution: Designing a join22s
-
-
-
-
-
-