Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When joining two simple tables on primary key columns and placing an addition equality condition this can be done in the join itself or the where clause.

For instance the following are equiavalent. My question is - is there any reason to use one style over the other?

SELECT * 
FROM A
INNER JOIN B ON A.A_ID = B.A_ID
            AND A.DURATION = 3.00

...vs:

SELECT * 
FROM A
INNER JOIN B ON A.A_ID = B.A_ID
WHERE A.DURATION = 3.00
like image 324
AJM Avatar asked Jan 14 '11 17:01

AJM


People also ask

Which is better join or WHERE clause?

“Is there a performance difference between putting the JOIN conditions in the ON clause or the WHERE clause in MySQL?” No, there's no difference. The following queries are algebraically equivalent inside MySQL and will have the same execution plan.

Are joins faster than 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.

Can WHERE clause be used with joins?

You join two tables by creating a relationship in the WHERE clause between at least one column from one table and at least one column from another. The join creates a temporary composite table where each pair of rows (one from each table) that satisfies the join condition is linked to form a single row.

When an equi join is performed?

1. EQUI JOIN : EQUI JOIN creates a JOIN for equality or matching column(s) values of the relative tables. EQUI JOIN also create JOIN by using JOIN with ON and then providing the names of the columns with their relative tables to check equality using equal sign (=).


3 Answers

Generally speaking it makes no semantic difference.

There is one edge case where it can do though. If the (deprecated) GROUP BY ALL construct is added to the query as illustrated below.

DECLARE @A TABLE(A_ID INT, DURATION DECIMAL(3,2) )
INSERT INTO @A VALUES(1,2.00)

DECLARE @B TABLE(A_ID INT)
INSERT INTO @B VALUES(1)

/*Returns one row*/
SELECT *
FROM @A A
INNER JOIN @B B ON A.A_ID = B.A_ID
WHERE A.DURATION = 3.00
GROUP BY ALL A.A_ID, A.DURATION, B.A_ID

/*Returns zero rows*/    
SELECT *
FROM @A A
INNER JOIN @B B ON A.A_ID = B.A_ID  AND A.DURATION = 3.00
GROUP BY ALL A.A_ID, A.DURATION, B.A_ID
like image 80
Martin Smith Avatar answered Oct 07 '22 19:10

Martin Smith


It's a style matter. Generally, you'd want to put the conditions that define the "shape" of the result set in the FROM clause (i.e. those that control which rows from each table should join together to produce a result), whereas those conditions which filter the result set should be in the WHERE clause. For INNER JOINs, the effects are equal, but once OUTER JOINs (LEFT, RIGHT) are involved, it feels a lot clearer.


In your first example, I'm left asking "what has this got to do with Table B?" when I encounter this odd condition in the JOIN. Whereas in the second, I can skip over the FROM clause (and all JOINs) if I'm not interested, and just see the conditions which determine whether rows are going to be returned in the WHERE clause.

like image 38
Damien_The_Unbeliever Avatar answered Oct 07 '22 18:10

Damien_The_Unbeliever


It's the type of JOIN that matters.
There's no difference, either version of the query provided will use the same execution plan because you are dealing with an INNER JOIN.

If dealing with an OUTER JOIN (IE: LEFT, RIGHT), there is a huge difference between the two versions because the WHERE criteria is applied after the JOIN is made. If the criteria is specified in the ON clause, the criteria is applied before the JOIN is made which can made a considerable difference between the result sets.

like image 6
OMG Ponies Avatar answered Oct 07 '22 19:10

OMG Ponies