Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NATURAL (JOIN) considered harmful in production environment?

I am reading about NATURAL shorthand form for SQL joins and I see some traps:

  • it just takes automatically all same named column-pairs (use USING to specify explicit column list)
  • if some new column is added, then join output can be "unexpectedly" changed too, which may be not so obvious (even if you know how NATURAL works) in complicated structures
like image 352
Grzegorz Szpetkowski Avatar asked May 18 '11 04:05

Grzegorz Szpetkowski


People also ask

What is natural join?

A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables. A NATURAL JOIN can be an INNER join, a LEFT OUTER join, or a RIGHT OUTER join. The default is INNER join.

Which operation is not allowed in join?

To be modifiable, a join view must not contain any of the following: Hierarchical query clauses, such as START WITH or CONNECT BY. GROUP BY or HAVING clauses. Set operations, such as UNION, UNION ALL, INTERSECT, MINUS.

Which clause is also used to find natural join of more than on table?

​The join clause is used to combine tables based on a common column and a join condition. A natural join is a type of join that combines tables based on columns with the same name and type.

Which are the join types in join condition?

There are four main types of JOINs in SQL: INNER JOIN, OUTER JOIN, CROSS JOIN, and SELF JOIN.


1 Answers

NATURAL JOIN syntax is anti-pattern:

  • The purpose of the query is less obvious;
    • the columns used by the application is not clear
    • the columns used can change "unexpectedly"
  • The syntax goes against the modularity rule, about using strict typing whenever possible. Explicit is almost universally better.

Because of this, I don't recommend the syntax in any environment.
I also don't recommend mixing syntax (IE: using both NATURAL JOIN and explicit INNER/OUTER JOIN syntax) - keep a consistent codebase format.

like image 159
OMG Ponies Avatar answered Sep 27 '22 21:09

OMG Ponies