Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fsxVc-00043p-Cn for pgsql-general@arkaria.postgresql.org; Thu, 23 Aug 2018 21:51:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fsxVZ-0006Gb-Ki for pgsql-general@arkaria.postgresql.org; Thu, 23 Aug 2018 21:51:13 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fsxVZ-0006G5-8x for pgsql-general@lists.postgresql.org; Thu, 23 Aug 2018 21:51:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fsxVS-0003Vk-S5 for pgsql-general@postgresql.org; Thu, 23 Aug 2018 21:51:12 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w7NLp3Jf012728; Thu, 23 Aug 2018 17:51:03 -0400 From: Tom Lane To: Mark Jeffcoat cc: pgsql-general@postgresql.org Subject: Re: Join condition parsing puzzle In-reply-to: References: Comments: In-reply-to Mark Jeffcoat message dated "Thu, 23 Aug 2018 13:17:37 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <12726.1535061063.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 23 Aug 2018 17:51:03 -0400 Message-ID: <12727.1535061063@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Mark Jeffcoat writes: > SELECT * > FROM relation_a > LEFT JOIN relation_b > JOIN relation_c > ON (relation_c.id_p =3D relation_b.id_p) > ON (relation_a.id_c =3D relation_b.id_c AND relation_a.id_v =3D relation= _b.id_v); > I would have claimed before seeing this example that it wasn't even > grammatical; I thought the only legal place to write the ON clause was > immediately after the JOIN. Apparently not. > How should I read this query? I'd appreciate any help understanding thi= s. You read it as SELECT * FROM relation_a LEFT JOIN (relation_b JOIN relation_c ON (relation_c.id_p =3D relation_b.id_p)) ON (relation_a.id_c =3D relation_b.id_c AND relation_a.id_v =3D relation= _b.id_v); There's no other valid way to parenthesize it, so that's what the parser does. regards, tom lane