Some SQL operators are as esoteric as they’re highly effective. One of many oldest operator that you just’ve doubtless infrequently utilized in actual world purposes is NATURAL JOIN which is the default in relational algebra. We’ve lined a cool use-case for NATURAL JOIN earlier on this weblog.
The primary purpose why it’s not very helpful is as a result of it joins two tables utilizing all column names from each joined tables. When you take a look at the Sakila database, this consists of audit columns like LAST_UPDATE, which is current on all tables. For instance, you wouldn’t need:
As a result of that will be equal to:
That is an apparent purpose, however even in the event you rigorously design your schema to by no means include two columns that “by chance” share their identify, somebody would possibly add one accidentally, nonetheless, which renders current queries ineffective.
JOIN USING to the rescue?
So, you’d suppose that the syntax that ought to actually be used as an alternative is JOIN .. USING? This seems to be good, doesn’t it?
And sure, that is actually cool for ad-hoc SQL. In case your schema is designed this manner (main key column identify = overseas key column identify), then this syntax can be utilized to rapidly be a part of tables throughout the schema.
However in an actual world utility, it’s best to nonetheless keep away from the syntax to stop disagreeable surprises when evolving the schema. Have a look at this script, for instance:
The question works completely positive as supposed. However what occurs if we add:
Now, there’s an ambiguity within the final JOIN. If we repeat the SELECT assertion from earlier, PostgreSQL complains:
ERROR: widespread column identify “j” seems greater than as soon as in left desk
Oracle confirms:
SQL Error [918] [42000]: ORA-00918: J: column ambiguously specified – seems in A and B
Why? As a result of we’re actually becoming a member of:
And since j will not be a be a part of column between a and b, it’s now ambiguous. Observe how the ALTER TABLE assertion broke a question that was completely positive earlier than. It might not have damaged if we used the JOIN .. ON syntax:
Too dangerous! Once more, when writing ad-hoc SQL for fast querying the Sakila database, JOIN .. USING is a useful gizmo – similar to SELECT *. However in manufacturing queries, this ad-hoc SQL instrument ought to be prevented.

