0

I would like to know what is the best way to create a connection between two tables (or more). I have seen two different ways to do so.

In the next example, I have 2 tables. Table1 has customerId (key) and customerName. Table2 has customerId (key) and CustomerPhone

table1:

 customerId | customerName|  
============+=============+
 1          |  Josh       |  
 2          |  Nadia      |   

table2:

 customerId | customerPhone|  
============+==============+
 1          |  123         |  
 2          |  456         |   

Which query is the best and why:

SELECT Table1.customerId, Table2.customerPhone  
FROM   Table1, Table2  
WHERE  Table1.customerId = Table2.customerId  

Query2:

 SELECT Table1.customerId, Table2.customerPhone  
 FROM   Table1   
 Inner Join  Table2 ON Table1.customerId = Table2.customerId   
nbk
  • 45,398
  • 8
  • 30
  • 47

3 Answers3

2

The second option is more common and considered to be the right one, however they are both doing the same thing under the hood

TomerRam
  • 31
  • 3
1

The first query makes first a cross join and then reduces the result to fit the where cflause.

The second matches the tables by check ing if the condition in the ON clause ist met.

Both do the same, but the first is slowe

nbk
  • 45,398
  • 8
  • 30
  • 47
1

The first query is written using old syntax (SQL-89):

SELECT Table1.customerId, Table2.customerPhone  
FROM   Table1, Table2  
WHERE  Table1.customerId = Table2.customerId  

The second query is written in modern syntax (SQL-92):

SELECT Table1.customerId, Table2.customerPhone  
FROM   Table1   
Inner Join  Table2 ON Table1.customerId = Table2.customerId   

They are equivalent. Use SQL-92. Use SQL-92!

The Impaler
  • 45,731
  • 9
  • 39
  • 76