user6901947
user6901947

Reputation: 107

using parenthesis in SQL

What's the differences between these SQLs:

SELECT *
FROM COURS NATURAL JOIN COMPOSITIONCOURS NATURAL JOIN PARCOURS; 

SELECT *
FROM COURS NATURAL JOIN (COMPOSITIONCOURS C JOIN PARCOURS P ON C.IDPARCOURS = P.IDPARCOURS) ;

SELECT *
FROM (COURS NATURAL JOIN COMPOSITIONCOURS C) JOIN PARCOURS P ON C.IDPARCOURS = P.IDPARCOURS ;

They have different results.

Upvotes: 0

Views: 70

Answers (2)

user6901947
user6901947

Reputation: 107

Thanks for all of yours answers! I finally find out that the problem is because of the use of natural JoIn. When using natural join, it will join every column with the same name! When I am using NATURAL JOIN for several times , it's possible those columns, which have same names but you don't wants to combine , could be joined automatically! Here is an example, Table a (IDa, name,year,IDb) Table b (IDb, bonjour,salute,IDc) Table c (IDc, year, merci) If I wrote like From a Natural Join b Natural c,because IDb and IDc are common names for Table a,b,c. It seems Ok! But attention please! a.year and b.year can also be joined ! That's what we don't want!

Upvotes: 0

SirSQL0
SirSQL0

Reputation: 37

It's difficult to tell precisely without a sample result set, but I would imagine its your utilization of natural JOINs which is typically bad practice. Reasons being:

I would avoid using natural joins, because natural joins are:

  • Not standard SQL.
  • Not informative. You aren't specifying what columns are being joined without referring to the schema.
  • Your conditions are vulnerable to schema changes. If there are multiple natural join columns and one such column is removed from a table, the query will still execute, but probably not correctly and this change in behavior will be silent.

Upvotes: 1

Related Questions