Lukas Eder has a nice post explaining different forms of automatic join elimination:
We intended to fetch all customers and their addresses. But observe: We project only columns from the
CUSTOMERtable and we don’t have any predicates at all, specifically not predicates using theADDRESStable. So, we’re completely ignoring any contributions from theADDRESStable. We never really needed the JOIN in the first place!And in fact, the optimiser can prove this too, because of the
FOREIGN KEYconstraint onC.ADDRESS_ID, which guarantees that everyCUSTOMERrecord has exactly one correspondingADDRESSrecord. The JOIN does not duplicate, nor remove anyCUSTOMERrows, so it is unneeded and thus eliminated (by some, not all databases, will list each database at the end of the article).So, the database can rewrite the SQL statement to the following, equivalent SQL statement in the presence of said FOREIGN KEY
Read on for a comparison across different products as well.