What is difference between JOINS and SUBQUERIES?Can anything we can do with joins can be done with subqueries also or vice-versa?

17,550

Solution 1

In most cases with enterprise applications, it's not just a question of IF something can be done, but HOW it is done. Generally speaking, Joins are faster and less expensive than subqueries. Ton of other posts on this subject on SO. Here's one: Join vs. sub-query

Solution 2

Frankly, I can't think of a case where one thing can't be achieved from either method (subquery or join).

To me it's more about readability and performance. For example, a subquery might be slower whereas a join might take advantage of certain indexes. At least from the DBMS standpoint, I would imagine, should be easier to optimize a join when analyzing the expression.

Consider the case of several joins expressed as subqueries, for example. To most people, seeing the statement expressed as a Left, Right or Inner join would make it easier to understand and maintain. I even avoid using implicit joins since they hide the intent. In other words, I prefer to express a join as from table a inner join table b on a.id=b.id vs from table a, table b where a.id=b.id. Implementing joins as subqueries make it even less readable, IMO.

Share:
17,550

Related videos on Youtube

Gautam Bhalla
Author by

Gautam Bhalla

A Software Professional by Profession,An Open Source Enthusiast by passion.

Updated on June 04, 2022

Comments

  • Gautam Bhalla
    Gautam Bhalla almost 2 years

    Possible Duplicate:
    SQL: Join vs. subquery

    Is there anything that we can do with joins but not with subqueries or vice-versa?

  • onedaywhen
    onedaywhen over 12 years
    "Generally speaking, Joins are faster and less expensive" -- But is there any value in generalizing?
  • onedaywhen
    onedaywhen over 12 years
    "To most people, seeing the statement expressed as a Left, Right or Inner join would make it easier to understand and maintain" -- I'm not sure that is correct for most, certainly not for me e.g. I much prefer to write a semijoin using EXISTS (subquery) rather than a join, ditto semi difference and I note many people on SO like to use IN (subquery).
  • Gautam Bhalla
    Gautam Bhalla over 12 years
    Is there any way to write correlated subqueries in oracle using join?
  • Gautam Bhalla
    Gautam Bhalla over 12 years
    Is there any way to write correlated subqueries in oracle using join?