0

So I have learned to join tables in SQL like this:

SELECT data1, data2 FROM table1, table2 WHERE table1.ID = table2.ID

Having problems with a task I researched and found sites that taught to use

SELECT table1.data1, table2.data2
FROM table1
INNER JOIN table2 ON table1.ID = table2.ID

Is there a difference between the two or do they just do the same? Which one should I use?

Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
Nivek770
  • 9
  • 1
  • 3
    *Never* use commas in the `FROM` clause. *Always* use proper, explicit `JOIN` syntax. – Gordon Linoff Jun 03 '17 at 16:57
  • they do the same work ..but you should use ever the secondo one .. the explicit join is clear .and easy to understand and reconize – ScaisEdge Jun 03 '17 at 17:06
  • 1
    It's good practice to use the second one. There are different forms of join: INNER JOIN, RIGHT JOIN, LEFT JOIN and OUTER JOIN. Use one based on your requirement. – Bhupesh Shrestha Jun 03 '17 at 18:00
  • Does this answer your question? [INNER JOIN ON vs WHERE clause](https://stackoverflow.com/questions/1018822/inner-join-on-vs-where-clause) – philipxy Mar 15 '21 at 22:56

1 Answers1

1

Both of your queries are equivalent in function. The second is strongly preferred: a query with explicit joins is more understandable, with implicit joins (your first query) the join constraints and the general filtering constraints are intermingled. Also, inner join is not the only join type: for outer joins it can be challenging to get the syntax correct when the constraints are in the where clause.