3

Possible Duplicates:
Difference between “and” and “where” in joins
In MySQL queries, why use join instead of where?

What is the difference between

SELECT * 
 from T1 
 JOIN T2 ON T1.X = T2.X 
        AND T1.Y = T2.Y

and

SELECT * 
  from T1 
  JOIN T2 ON T1.X = T2.X
 WHERE T1.Y = T2.Y

?

Community
  • 1
  • 1
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • 2
    Exact duplicate? http://stackoverflow.com/questions/2559194/difference-between-and-and-where-in-joins Near Duplicate: http://stackoverflow.com/questions/2722795/in-sql-mysql-what-is-the-difference-between-on-and-where-in-a-join-stateme – KevenK Aug 30 '10 at 19:53
  • Both should return the same result set, although they may use different execution plans. – Robert Harvey Aug 30 '10 at 19:53
  • 2
    There isn't a difference, but there would be if this was using an OUTER join... – OMG Ponies Aug 30 '10 at 19:56

3 Answers3

1

The difference is only in outer joins. I'd challenge you to parse this for simplicity -- I think the mental process you will go through will help clear this up. These can be rewritten in INNER JOINS and often are.

SELECT * FROM t1
LEFT OUTER JOIN t2
  ON ( true )
  WHERE false

-vs-

SELECT * FROM t1
LEFT OUTER JOIN t2
  ON ( false )
  WHERE true
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
0

The where clause usually scans the results of any joins and filters those, thus potentially making it slower. However, with modern query optimizers, this could potentially be pushed earlier into the join process, making it just as fast/equivalent. I just opt to go for the more "correct" route of joining during on so I don't have to worry about what the optimizer is doing.

Note: the where clause can also have all kinds of conditions on the join such as

where t1.id = t2.id OR t1.pickmynose = 'TRUE'

This is why its available there (I think), but shouldn't be used unless needed.

Zak
  • 24,947
  • 11
  • 38
  • 68
0

The way that you're using them (inner join), I don't think that there will be any. You would see a difference if you were using outer joins, since the ON clause would not filter out rows, but rather return rows with T1 or T2's fields all NULL.

ngroot
  • 1,186
  • 5
  • 11