SQL Query Question: Difference between using JOIN and = (equal)?

by heru on August 27, 2011

Query #1:

2
SELECT * FROM a, b WHERE a.b_id = b.id AND b.id > 0;

vs

Query #2:

2
SELECT * FROM a INNER JOIN b ON a.b_id = b.id AND b.id > 0;
  1. Are they the same?
  2. Do they produce the same result?
  3. Which query runs faster?
The third one is actually a trick question. The answer for #1 and #2 is that technically both are the same. I have found this out of curiosity after doing an interview with fresh graduates that have the habit of using INNER JOIN instead of =.
After researching through, I found out that a thread in stack overflow discusses this in pretty details. Please make sure your read other answers as well!
Basically in summary from StackOverflow:
  1. #2 is easier to read when you have multiple tables involved.
  2. #2 is safer because #1 if you miss the ‘=’, then it will cause a cartesian join (which in 2 large tables, it will be very painful!
I am found guilty that I am a habitual practice of #1. I think it is time to break my old habit and adapt a new one.

Related Posts

  • No Related Post

Previous post:

Next post: