Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and when a LEFT JOIN with condition in WHERE clause is not equivalent to the same LEFT JOIN in ON? [duplicate]

Tags:

I'm experiencing a very confusing situation that makes me question all my understanding of joins in SQL Server.

SELECT t1.f2  FROM   t1  LEFT JOIN t2  ON t1.f1 = t2.f1 AND cond2 AND t2.f3 > something  

Does not give the same results as :

SELECT t1.f2  FROM   t1  LEFT JOIN t2  ON t1.f1 = t2.f1 AND cond2  WHERE  t2.f3 > something  

Can please someone help by telling if this two queries are supposed to be equivalent or not?

Thx

like image 306
Kamel Keb Avatar asked Mar 29 '13 15:03

Kamel Keb


People also ask

What is the difference between left join with WHERE clause and left join without WHERE clause?

When you use a Left Outer join without an On or Where clause, there is no difference between the On and Where clause. Both produce the same result as in the following. First we see the result of the left join using neither an On nor a Where clause.

Why Left join and not inner join?

You'll use INNER JOIN when you want to return only records having pair on both sides, and you'll use LEFT JOIN when you need all records from the “left” table, no matter if they have pair in the “right” table or not.

Can we use LEFT join without on condition?

You can indeed implement this using LEFT JOIN . For LEFT JOIN you must have ON but you can use ON TRUE .


1 Answers

The on clause is used when the join is looking for matching rows. The where clause is used to filter rows after all the joining is done.

An example with Disney toons voting for president:

declare @candidates table (name varchar(50)); insert @candidates values      ('Obama'),      ('Romney'); declare @votes table (voter varchar(50), voted_for varchar(50)); insert @votes values      ('Mickey Mouse', 'Romney'),     ('Donald Duck', 'Obama');  select  * from    @candidates c left join             @votes v on      c.name = v.voted_for         and v.voter = 'Donald Duck' 

This still returns Romney even though Donald didn't vote for him. If you move the condition from the on to the where clause:

select  * from    @candidates c left join             @votes v on      c.name = v.voted_for where   v.voter = 'Donald Duck' 

Romney will no longer be in the result set.

like image 128
Andomar Avatar answered Sep 30 '22 06:09

Andomar