Why is the order of tables important when combining an outer & an inner join ? the following fails with postgres:
SELECT grp.number AS number, tags.value AS tag FROM groups grp, insrel archiverel LEFT OUTER JOIN ownrel ownrel ON grp.number = ownrel.dnumber LEFT OUTER JOIN tags tags ON tags.number = ownrel.snumber WHERE archiverel.snumber = 11128188 AND archiverel.dnumber = grp.number
with result:
ERROR: invalid reference to FROM-clause entry for table 'grp' LINE 5: LEFT OUTER JOIN ownrel ownrel ON grp.number = ownrel.d... ^ HINT: There is an entry for table 'grp', but it cannot be referenced from this part of the query.
when the groups are reversed in the FROM it all works:
SELECT grp.number AS number, tags.value AS tag FROM insrel archiverel, groups grp LEFT OUTER JOIN ownrel ownrel ON grp.number = ownrel.dnumber LEFT OUTER JOIN tags tags ON tags.number = ownrel.snumber WHERE archiverel.snumber = 11128188 AND archiverel.dnumber = grp.number
I believe that you can think of this as an operator precedence issue.
When you write this:
I think it is interpreted by the parser like this:
If so, then in the innermost join ‘grp’ is unbound.
When you reverse the lines with ‘groups’ and ‘insrel’, the innermost join applies to ‘groups’ and ‘ownrel’, so it works.
Probably this would work as well: