2

I'm just curious, if i have table a and table b.

I write query 1:

SELECT * FROM table a INNER JOIN table b ON table a.id = table b.id 

I write query 2:

SELECT * FROM table b INNER JOIN table a ON table b.id = table a. id 

What is the difference both of above query?

Thank you

2
  • There is no difference,in the results at least. Commented Feb 4, 2014 at 11:10
  • heheh...exactly thank you Commented Feb 5, 2014 at 0:25

2 Answers 2

2

When using INNER JOIN , there is no difference in resultset returned except in order of columns when SELECT * is used i.e. columns are not explicitly mentioned.

SELECT * FROM table a INNER JOIN table b ON table a.id = table b.id 

returns columns from tableA followed by columns from tableB

SELECT * FROM table b INNER JOIN table a ON table b.id = table a. id 

returns columns from tableB followed by columns from tableA

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

2 Comments

This is correct. Furthermore, on SQL Server, both queries will perform equally well, no matter the size of each table, thanks to the query optimizer.
I think there is a general knowledge of sql server in stack overflow's users.
0

The second table matches data with the first one. So it is better to put smaller table on the second place.

3 Comments

not necessarily since there is no where... it is still pulling all records from both tables where the join matches... and if criteria, I would ensure index and query from the larger table listed first.
For major DBSM there's no difference, the engine ill choose the order to do nested loop based on internal statistics. For MS SQL I'm sure it ill work at least with seven (the mileage can vary) joins.
Yes optimizer would decide in different ways.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.