-2

Possible Duplicate:
Is it better to do an equi join in the from clause or where clause

I have following two SQL queries. Can you please explain me the differences between these two ?

QUERY: 1

SELECT a.*, b.*
FROM Table1 a
INNER JOIN Table2 b
ON a.id = b.id
AND a.col = 'value'

QUERY: 2

SELECT a.*, b.*
FROM Table1 a
INNER JOIN Table2 b
ON a.id = b.id
WHERE a.col = 'value'

Thanks

2
  • The only time this makes any difference for an INNER JOIN is if you are using GROUP BY ALL Dec 6, 2011 at 21:49
  • @MartinSmith good catch on the dupe, I couldn't find one
    – JNK
    Dec 6, 2011 at 21:51

5 Answers 5

4

Nothing unless case sensitivity is turned on on the database

1
  • I am looking AND and WHERE conditions. Which one i have to use over other and when ? Dec 6, 2011 at 21:47
2

For an INNER JOIN, filtering on the JOIN condition vs filtering in the WHERE clause should give identical results.

If this were an OUTER JOIN, the results would be different since the first one pre-filters the results in the source table before the JOIN condition.

1

in this particular case it's the same result, but it's not the same thing. the WHERE applies to the whole select, while the ON ... AND is only for the inner join (actually probably the same thing, but not for outer joins)

0

Yes, everything is pretty much the same. Case sensitive database will return error if your ID fields is not spelled correctly.

0

The difference is purely stylistic. Personally, I like to put the clauses that are key to performing the table joins up under the 'JOIN ... ON a=b' section, and put the clauses that have to do with my specific query filtering under the "WHERE" clause. There is no technical reason to do it one way or the other though, so long as you're talking about inner joins.

Not the answer you're looking for? Browse other questions tagged or ask your own question.