Re: Left join syntax error

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Erik Wienhold <ewie(at)ewie(dot)name>
Cc: Shammat <shammat(at)gmx(dot)net>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Left join syntax error
Date: 2024-05-18 15:16:58
Message-ID: 2762582.1716045418@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Erik Wienhold <ewie(at)ewie(dot)name> writes:
> But I wonder if the implicit cross join syntax ("FROM peoples, companies")
> should actually produce this error because the explicit cross join
> works:

> SELECT p.lname, p.fname, p.job_title, p.company_nbr, p.email, c.company_name
> FROM people as p
> CROSS JOIN companies as c
> LEFT JOIN companies ON c.company_nbr = p.company_nbr;

> But I'm not even sure if implicit and explicit cross join are
> semantically equivalent.

Well, they do the same thing, but JOIN binds tighter than comma.
So in one case you have effectively

FROM people as p CROSS JOIN
(companies as c LEFT JOIN companies ON c.company_nbr = p.company_nbr)

and "p" is not within the scope of the JOIN/ON clause.
The other way is effectively

FROM (people as p CROSS JOIN companies as c)
LEFT JOIN companies ON c.company_nbr = p.company_nbr;

which is syntactically legal, although it probably doesn't do
what you wanted.

If memory serves, MySQL got this basic syntactic detail wrong
for years, as a result of which there's (still) a tremendous amount
of confusion on the net about what is the syntactic precedence in
FROM clauses.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rich Shepard 2024-05-18 15:18:34 Re: Left join syntax error
Previous Message Erik Wienhold 2024-05-18 15:14:32 Re: Left join syntax error