Smruti Ranjan Sahoo
Smruti Ranjan Sahoo

Reputation: 254

Which is better Sub-Query or JOIN in SQl query?

Can any one suggest which one is better in this case:

1:

SELECT
    COLUMN1, 
        (SELECT 
            COLUMN2 
        FROM
            TABLE2
        WHERE 
            <some condition>) AS COLUMN2
FROM TABLE 1 

2:

SELECT
    T1.COLUMN1,
    T2.COLUMN2
FROM
    TABLE1 T1
INNER JOIN 
    TABLE2 T2 ON <some condition>

Upvotes: 3

Views: 1694

Answers (4)

jmoreno
jmoreno

Reputation: 13571

There are several axis of "better"...

  • Better performance
  • Better readability
  • Better maintainability
  • Better flexibility ....

    So,

    1. Test and find out (examine plans they may be identical)
    2. In the eye of the beholder, where the queries are equvalent I prefer joins
    3. Depends on your staff
    4. Doesn't seem applicable in this case ...

Better is frequently just a trade off...

Upvotes: 4

xdazz
xdazz

Reputation: 160943

Which is better?

It is not the problem which is better, because they are complete different query.

Your first query will be error if your sub query return more than 1 row.

Upvotes: 2

Spencer Ruport
Spencer Ruport

Reputation: 35117

Use JOIN.

Subqueries vs joins

Upvotes: 3

Related Questions