relational-databasecartesian-productrelational-algebranatural-join

Relational Algebra - how does natural join work?


A natural join is an inner join that only works if table1 has some intersecting attributes with table2.

Yet, when I take tables that have no column names in common, it acts as a Cartesian product.

In addition, when I take different tables that have nothing in common, it displays no results.

Why?


Solution

  • Well, you have learned the first important lesson, which is to avoid natural join. It is just lousy syntax, because it does not even take properly declared foreign key relationships into account and the join conditions are hidden -- which makes queries hard to maintain and debug.

    A natural join is an inner join equijoin with the join conditions on columns with the same names. Natural joins do not even take types into account, so the query can have type conversion errors if your data is really messed.

    If the corresponding inner join on the common column names have no matches, then it returns the empty set. If there are no common column names, then it is the same as a cross join.

    The way to think about it is that a natural join (inner natural join) generates the Cartesian product of two tables. When the tables have duplicated column names, then the final result set contains only those Cartesian-product rows where the common column names have the same value.