Q

Difference between subquery and join

When comparing subqueries and joins, which one is faster?

When comparing subqueries and joins, which one is faster?

The best answer is: it depends. It depends on which database system you're using. It depends on how sophisticated the optimizer is. It depends on which indexes you have defined on the columns. It depends on how complex the query is. It depends on whether the current month name has an R in it. Okay, I was kidding about that last one.

Here are two interesting articles that touch this issue in slightly more detail than I would:

The final answer? You will have to run performance tests on your own tables with your own subqueries and joins, and compare the results. That was my advice in NOT EXISTS correlated subquery, or OUTER JOIN? (20 September 2004).

That said, my preference is often for the subquery. A subquery just seems to convey the semantics of what the query is trying to achieve better than a join. For an example, see my previous answer Subquery or join? (20 October 2003).

This was first published in November 2005

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close