Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Subquery or join?
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Subquery or join?

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 20 October 2003
Some tasks can be performed in two ways, both by joins and subqueries. Under what situations should we opt for subqueries?

>

Write your query using a subquery instead of a join when it is easier for you to understand what the purpose or intent of the query is. Remember, you not only have to write it, you may have to come back six months later and figure out what it's doing! I'll demonstrate with two examples:

Example 1

Do you prefer:

select a.foo, a.bar
  from table1 a
inner
  join table2 b
    on a.id = b.id
   and b.qux = '937'
group
    by a.foo, a.bar

or:

select foo, bar
  from table1 
 where id in
       ( select id
           from table2
          where qux = '937' )

Example 2

Do you prefer:

select a.foo, a.bar
  from table1 a
left outer
  join table2 b
    on a.id = b.id
   and b.qux = '937'
 where b.id is null

or:

select foo, bar
  from table1 
 where not exists
       ( select 1
           from table2
          where id = table1.id
            and qux = '937' )

Ordinarily, when writing SQL, you should not be concerned with performance. Instead, you should focus all your intellect on ensuring that you get the correct results, and let the database optimizer figure out how to satisfy the query. So the first consideration is whether the join and subquery techniques actually do both produce the same correct results in either example.

The other consideration is maintainability. The first example returns rows from table1 with matching qualified rows in table2, while the second example returns only rows without qualified matches. Now consider how you would have to modify each query in both examples to add another condition, to return only those rows from table1 that also have a qualifying matching row in table3. You may discover that the join technique is severely limiting.

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts