25

I would like to know which one is best regarding performance between the 2 queries stated below or do they perform identically?

First one: [without WHERE clause, just AND with ON]

SELECT related_tabid AS tabid, label, t.name
  FROM relatedlists r
     INNER JOIN tab t 
       ON t.tabid = r.tabid
          AND t.name = 'Leads'
          AND r.is_active=1  and r.related_tabid <> 0
          AND t.is_active=1
  ORDER BY label

Second one: [using WHERE clause, AND associated with where instead of ON ]

SELECT related_tabid AS tabid, label, t.name
  FROM relatedlists r
     INNER JOIN tab t 
       ON t.tabid = r.tabid
       WHERE t.name = 'Leads'
             AND r.is_active=1  and r.related_tabid <> 0
             AND t.is_active=1
  ORDER BY label

3 Answers 3

17

Both queries are the same because the join used is INNER JOIN. INNER JOIN basically it filters only rows that has at least a match on the other table. Even the two tables are interchange, the result is still the same.

But if you are joining them via LEFT JOIN, the two queries are different from each other and will yield different result.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your response. if i use LEFT JOIN which one will be faster in performance?
a good article for your question: INNER JOIN vs LEFT JOIN performance
3

It looks to me that the only difference between the two queries is that one has the t.name = 'Leads' in the WHERE clause and one has it in the JOIN clause. Correct?

There is no difference between the two. The SQL optimizer will handle both of them identically. Do an EXPLAIN on each of them to verify.

Comments

1

In theory, the first query is well just be wanting to put text comparison in the 'where', then joins in the definition of the 'logic' of union of tables and the 'where' is only used to specify values that vary when comparing Where t.name = 'Leads'; regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.