John Smith
John Smith

Reputation: 625

SQL Natural Join 2 matching columns

What if 2 columns have the same name in a natural join of 2 tables? Which tuples will be selected, is it enough if one column value is the same or does it require both columns then

Upvotes: 2

Views: 2592

Answers (1)

IMSoP
IMSoP

Reputation: 97848

 Table1 NATURAL JOIN Table2

is equivalent to

 Table1 JOIN Table2
 ON Table1.Col1 = Table2.Col1
 AND Table1.Col2 = Table2.Col2
 ...

with all column names which exist in both tables being added to the ON clause.

So yes, since it is an AND not an OR which is implied, they must all match.

This is useful if you have a 2-part Key, e.g. company_code + username, and a hazard if you have standard named non-key fields, such as date_created.

Upvotes: 5

Related Questions