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 1s8LXm-00Bx3q-JY for pgsql-general@arkaria.postgresql.org; Sat, 18 May 2024 15:00:20 +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 1s8LXl-00747v-Vh for pgsql-general@arkaria.postgresql.org; Sat, 18 May 2024 15:00:17 +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 1s8LXl-00747k-FN for pgsql-general@lists.postgresql.org; Sat, 18 May 2024 15:00:17 +0000 Received: from mout-u-107.mailbox.org ([80.241.59.207]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s8LXh-000pci-Kq for pgsql-general@lists.postgresql.org; Sat, 18 May 2024 15:00:16 +0000 Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4VhRnz20Wtz9sr2; Sat, 18 May 2024 17:00:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ewie.name; s=MBO0001; t=1716044407; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QPT+E4GKs5InaA8tdDb0RnRBCAJNHy1FVPjM6GxcI9s=; b=RsWEfveFIokDOay6JpDf6XJbufm1G4kzLmUJFdNq2JVfNr1nDVzb6fdTWB7R5teCE2jyyZ caz+5XwWaJr7WQ7cIwyMSlFeVUSlLfuAmeZ2dLY6KsKxJwuVzkCPwfvlUfNssPUf80xASz whjTIs6/iYKClzrsWjhbo3fw+jbrVZgw6c72XDtPKceNFtlkP5KXnr+8XhR7aFZ/sqvIp9 /shiNOqYVoCAxScgSLzXDM/VP84PhoW2lrgm0a2aAUVh3ANUXoz0wMy0Ks+XSIIz01OxWn A56n7oOt2umeoj+HYKlI67BN9Cmf0DA+4lRviDizjKd7ZA3uOtTNSgqkkiwuzA== Date: Sat, 18 May 2024 17:00:05 +0200 From: Erik Wienhold To: Shammat Cc: pgsql-general@lists.postgresql.org Subject: Re: Left join syntax error Message-ID: <7d422a60-c581-485d-b5fd-4b2bb284b919@ewie.name> References: <2c877258-61cc-dd2d-fac7-4f2a5c6293e7@appl-ecosys.com> <9d899286-3a73-4894-a6e0-eab529c92e65@gmx.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <9d899286-3a73-4894-a6e0-eab529c92e65@gmx.net> X-Rspamd-Queue-Id: 4VhRnz20Wtz9sr2 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 = 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 = 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 = 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 = 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: "If multiple sources are specified, the result is the Cartesian product (cross join) of all the sources." Maybe it's only meant that both syntaxes are equivalent regarding the result, and that it does not extend to aliases of those FROM items. If you just move the LEFT JOIN condition to the WHERE clause it works as well, which indicates that the aliases from the implicit cross join do work as if it has been an explicit cross join: 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 true WHERE c.company_nbr = p.company_nbr; [1] https://www.postgresql.org/docs/current/sql-select.html#SQL-FROM -- Erik