Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the order of natural joins matter

Tags:

sql

If I say:

A NATURALJOIN B

Is that the same as:

B NATURALJOIN A

Similarly, if I say:

A NATURALJOIN B NATURALJOIN C

How is that supposed to be evaluated?

like image 890
Ogen Avatar asked Mar 24 '14 04:03

Ogen


1 Answers

Here are my remarks:

  1. NATURALJOIN is two words: NATURAL JOIN
  2. The order of the tables makes no difference. Exception: when you SELECT *, then the list of selected columns is all columns of the first table, then all columns of the second table.
  3. Don't use NATURAL JOINs. It joins tables by their common columns. If the tables happen to share a column name like "text", "description" or "name" this will be used. Imagine you write a program with a SELECT statement, where two tables person and job are joined by person_id. Later you add a column "description" to both tables. Suddenly your program will fail completely to get any matching record, because "person_id" may match, but "description" won't.
like image 63
Thorsten Kettner Avatar answered Oct 08 '22 05:10

Thorsten Kettner