Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s8Ljk-00ByOz-Ef for pgsql-general@arkaria.postgresql.org; Sat, 18 May 2024 15:12:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s8Ljk-007Wtj-Ab for pgsql-general@arkaria.postgresql.org; Sat, 18 May 2024 15:12:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s8Ljj-007Wtb-Rz for pgsql-general@lists.postgresql.org; Sat, 18 May 2024 15:12:40 +0000 Received: from mail-oo1-xc30.google.com ([2607:f8b0:4864:20::c30]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s8Ljh-000pk8-9h for pgsql-general@lists.postgresql.org; Sat, 18 May 2024 15:12:38 +0000 Received: by mail-oo1-xc30.google.com with SMTP id 006d021491bc7-5b27d9fe710so568679eaf.3 for ; Sat, 18 May 2024 08:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716045156; x=1716649956; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=GnWdkedV70pIHtF6yM12Rb5+65uTG2BjYKCja+vh/Wg=; b=SHog7ucf7br2vL52SC14TbNcSROrVrjIO90IKmaHTJnp2ZJ6Jm2FFCAmplmss4QXFm o9tutUL4B3qkyUiN548lgEUmWB6wPsqJ68qjeCJZao569zteuVX69kyxQaPNQ/yrwL+v pwq7Z9q/ctWTVAXxbYuh0LcKJaExXbZT/tQgx+PRwNCvRK5wjruCMQ7/ALG9NCxW1Rmj prSLV0n8Y4mkBCSpE51usEnlv6NMEzpgmoao/xexhsYN2BQVQAAwuiy1ohvYnbHhRQ6h M3kyIQ2qn/+nfoqbI0UevdL2pu8IAyQUdrY6OcnT4SSvWdr/MmdIZwHsbrhzSd7sFxP9 FJaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716045156; x=1716649956; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GnWdkedV70pIHtF6yM12Rb5+65uTG2BjYKCja+vh/Wg=; b=Np+7hCxVGfYL+zrbDCXVprD5deeX03bFdRl/S08e6OnjmSBn+0Ul4V8MxB9ZA5gBys S+/Ra9gNqAwC51M61uHxYzlCyYOIQ06nWM1Gbhzk9o68N2ZpJQhP8qVR9bYtYpCYLUB2 gQ5WowSoYovC3i0XSKOzwareQ3lly12NjZnpiAsnegLW91jorX82CBKLM6CkwcKMSAUh VgFDlGxToqYjpJnMH4dpZpsVyXsrz1zMBmq1M13l/5U6yJJgQ+03OkbZi89ZHOasBPIV BJzljQFJJyt8DdAeiZpu9hN8WBS+7Lnvf+ZZXhhb+XIgJ1XB1nyoMDJYRqVBvadcdD6z eR0Q== X-Forwarded-Encrypted: i=1; AJvYcCWqP64ElC34AD9q5EONrXZEKATC6M39o+C/803BkwQjpC/JyLql4PEOcY1sQ41YPas2RTfY5SjF0LZEjZz7JCS7JuVEhofszYvDv+cAdzYbwZEq X-Gm-Message-State: AOJu0Yw5iKIIbIOp7Eqp/rEC55MfLgGwFK7iOFFwO6ZfJZ2Ka7OWhCw2 toiTxDL8adJJ9J5jZdPgdJog/D+0zhGTOZSfiaA9i5CCsy9qjBA/5aMvtwbAs+uHtxMyYpuPBBC OfOVJGpITzkCgxqVfdBqZ0AL4edQ= X-Google-Smtp-Source: AGHT+IEuKVtsV3zSQOH/4uQcmn4ALKYSSU7MCbAbGM78CuWeINDRmCLgZP3mgP/THV+6+qFKjKll3QfUpshbAuo9DTw= X-Received: by 2002:a4a:4881:0:b0:5b2:ef4:873d with SMTP id 006d021491bc7-5b28196c378mr25472971eaf.4.1716045156310; Sat, 18 May 2024 08:12:36 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a8a:156:0:b0:52c:cea1:1841 with HTTP; Sat, 18 May 2024 08:12:35 -0700 (PDT) In-Reply-To: <7d422a60-c581-485d-b5fd-4b2bb284b919@ewie.name> References: <2c877258-61cc-dd2d-fac7-4f2a5c6293e7@appl-ecosys.com> <9d899286-3a73-4894-a6e0-eab529c92e65@gmx.net> <7d422a60-c581-485d-b5fd-4b2bb284b919@ewie.name> From: "David G. Johnston" Date: Sat, 18 May 2024 08:12:35 -0700 Message-ID: Subject: Re: Left join syntax error To: Erik Wienhold Cc: Shammat , "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000f5e6800618bbe5e5" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000f5e6800618bbe5e5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Saturday, May 18, 2024, Erik Wienhold wrote: > On 2024-05-18 15:19 +0200, Shammat wrote: > > Am 18.05.24 um 14:52 schrieb Rich Shepard: > > > It's been a _very_ long time since I wrote a SQL script and, despite > looking > > > at my SQL books and web pages, I don't know how to fix the error. > > > > > > The three line script is: > > > ----- > > > SELECT p.lname, p.fname, p.job_title, p.company_nbr, p.email, > c.company_name > > > FROM people as p, companies as c > > > LEFT JOIN companies ON c.company_nbr =3D p.company_nbr; > > > ----- > > > > > > and psql responds: > > > ERROR: invalid reference to FROM-clause entry for table "p" > > > LINE 3: LEFT JOIN companies ON c.company_nbr =3D p.company_nbr; > > > ^ > > > HINT: There is an entry for table "p", but it cannot be referenced > from this part of the query. > > > > Don't put the second table in the FROM part > > > > SELECT p.lname, p.fname, p.job_title, p.company_nbr, p.email, > c.company_name > > FROM people as p > > LEFT JOIN companies as c ON c.company_nbr =3D p.company_nbr > > Yes, Rich probably just wants the left join. > > 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 =3D p.company_nbr; > > But I'm not even sure if implicit and explicit cross join are > semantically equivalent. The docs on FROM [1] sort of imply that: > Too lazy to find the docs right now but what you are observing is basically an operator precedence effect. The comma join hasn=E2=80=99t happened at t= he time the left join is evaluated and so other tables in the comma join cannot appear in the on clause of the left join. Placing everything inside a single from slot and moving the conditions to the where clause removes changes the precedence aspect so that the cross join does indeed evaluate prior to the left join. I=E2=80=99m content with not pointing out this possible gotcha in the docum= entation. David J. --000000000000f5e6800618bbe5e5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Saturday, May 18, 2024, Erik Wienhold <ewie@ewie.name> wrote:
On 2024= -05-18 15:19 +0200, Shammat wrote:
> Am 18.05.24 um 14:52 schrieb Rich Shepard:
> > It's been a _very_ long time since I wrote a SQL script and, = despite looking
> > at my SQL books and web pages, I don't know how to fix the er= ror.
> >
> > The three line script is:
> > -----
> > SELECT p.lname, p.fname, p.job_title, p.company_nbr, p.email, c.c= ompany_name
> >=C2=A0 =C2=A0 FROM people as p, companies as c
> > LEFT JOIN companies ON c.company_nbr =3D p.company_nbr;
> > -----
> >
> > and psql responds:
> > ERROR:=C2=A0 invalid reference to FROM-clause entry for table &qu= ot;p"
> > LINE 3: LEFT JOIN companies ON c.company_nbr =3D p.company_nbr; > >=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= ^
> > HINT:=C2=A0 There is an entry for table "p", but it can= not be referenced from this part of the query.
>
> Don't put the second table in the FROM part
>
> SELECT p.lname, p.fname, p.job_title, p.company_nbr, p.email, c.compan= y_name
> FROM people as p
>=C2=A0 =C2=A0LEFT JOIN companies as c ON c.company_nbr =3D p.company_nb= r

Yes, Rich probably just wants the left join.

But I wonder if the implicit cross join syntax ("FROM peoples, compani= es")
should actually produce this error because the explicit cross join
works:

=C2=A0 =C2=A0 SELECT p.lname, p.fname, p.job_title, p.company_nbr, p.email,= c.company_name
=C2=A0 =C2=A0 FROM people as p
=C2=A0 =C2=A0 =C2=A0 =C2=A0 CROSS JOIN companies as c
=C2=A0 =C2=A0 =C2=A0 =C2=A0 LEFT JOIN companies ON c.company_nbr =3D p.comp= any_nbr;

But I'm not even sure if implicit and explicit cross join are
semantically equivalent.=C2=A0 The docs on FROM [1] sort of imply that:

Too lazy to find the docs right now but wh= at you are observing is basically an operator precedence effect.=C2=A0 The = comma join hasn=E2=80=99t happened at the time the left join is evaluated a= nd so other tables in the comma join cannot appear in the on clause of the = left join.=C2=A0 Placing everything inside a single from slot and moving th= e conditions to the where clause removes changes the precedence aspect so t= hat the cross join does indeed evaluate prior to the left join.
<= br>
I=E2=80=99m content with not pointing out this possible gotch= a in the documentation.

David J.


--000000000000f5e6800618bbe5e5--