Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INNER JOIN condition in WHERE clause or ON clause?

I mistyped a query today, but it still worked and gave the intended result. I meant to run this query:

SELECT e.id FROM employees e JOIN users u ON u.email=e.email WHERE u.id='139840'

but I accidentally ran this query

SELECT e.id FROM employees e JOIN users u ON u.email=e.email AND u.id='139840'

(note the AND instead of WHERE in the last clause)

and both returned the correct employee id from the user id.

What is the difference between these 2 queries? Does the second form only join members of the 2 tables meeting the criteria, whereas the first one would join the entire table, and then run the query? Is one more or less efficient than the other? Is it something else I am missing?

Thanks!

like image 242
chiliNUT Avatar asked Jan 17 '14 21:01

chiliNUT


People also ask

Should I put condition in join or WHERE clause?

It is better to add the condition in the Join. Performance is more important than readability.

Can we use inner join in WHERE clause?

To use the WHERE clause to perform the same join as you perform using the INNER JOIN syntax, enter both the join condition and the additional selection condition in the WHERE clause. The tables to be joined are listed in the FROM clause, separated by commas. This query returns the same output as the previous example.

Can I use WHERE before inner join?

The where clause will be executed before the join so that it doesn't join unnecessary records. So your code is fine the way it is.

Which is faster inner join or WHERE clause?

The subquery can be placed in the following SQL clauses they are WHERE clause, HAVING clause, FROM clause. Advantages Of Joins: The advantage of a join includes that it executes faster. The retrieval time of the query using joins almost always will be faster than that of a subquery.


2 Answers

For inner joins like this they are logically equivalent. However, you can run in to situations where a condition in the join clause means something different than a condition in the where clause.

As a simple illustration, imagine you do a left join like so;

select x.id
from x
       left join y
         on x.id = y.id
;

Here we're taking all the rows from x, regardless of whether there is a matching id in y. Now let's say our join condition grows - we're not just looking for matches in y based on the id but also on id_type.

select x.id
from x
       left join y
         on x.id = y.id
         and y.id_type = 'some type'
;

Again this gives all the rows in x regardless of whether there is a matching (id, id_type) in y.

This is very different, though:

select x.id
from x
       left join y
         on x.id = y.id
where y.id_type = 'some type'
;

In this situation, we're picking all the rows of x and trying to match to rows from y. Now for rows for which there is no match in y, y.id_type will be null. Because of that, y.id_type = 'some type' isn't satisfied, so those rows where there is no match are discarded, which effectively turned this in to an inner join.

Long story short: for inner joins it doesn't matter where the conditions go but for outer joins it can.

like image 158
yieldsfalsehood Avatar answered Oct 04 '22 03:10

yieldsfalsehood


In the case of an INNER JOIN, the two queries are semantically the same, meaning they are guaranteed to have the same results. If you were using an OUTER join, the meaning of the two queries could be very different, with different results.

Performance-wise, I would expect that these two queries would result in the same execution plan. However, the query engine might surprise you. The only way to know is to view the execution plans for the two queries.

like image 36
Joel Coehoorn Avatar answered Oct 04 '22 05:10

Joel Coehoorn