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 1fa0XN-0008Oq-CK for pgsql-general@arkaria.postgresql.org; Mon, 02 Jul 2018 15:14:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fa0XL-0000HC-8I for pgsql-general@arkaria.postgresql.org; Mon, 02 Jul 2018 15:14:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fa0We-0005Hg-Cd for pgsql-general@lists.postgresql.org; Mon, 02 Jul 2018 15:14:00 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fa0Wa-0006fg-TX for pgsql-general@postgresql.org; Mon, 02 Jul 2018 15:13:59 +0000 Received: from pro.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w62FDo28005775; Mon, 2 Jul 2018 11:13:50 -0400 From: Tom Lane To: pinker cc: pgsql-general@postgresql.org Subject: Re: except all & WITH - syntax error? In-reply-to: <1530541501991-0.post@n3.nabble.com> References: <1530541501991-0.post@n3.nabble.com> Comments: In-reply-to pinker message dated "Mon, 02 Jul 2018 07:25:01 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <63779.1530544433.1@sss.pgh.pa.us> Date: Mon, 02 Jul 2018 11:13:53 -0400 Message-ID: <63780.1530544433@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk pinker writes: > Something strange happened to me right now. I'm trying to compare results > from one query with rewritten version and everything is ok with this order: > WITH abc AS (SELECT 1) SELECT 1 > except all > SELECT 1 > but when I'm trying other way around it throws an error: > SELECT 1 > except all > WITH abc AS (SELECT 1) SELECT 1 You need some parens: # SELECT 1 except all (WITH abc AS (SELECT 1) SELECT 1); ?column? ---------- (0 rows) In your first example, the WITH actually attaches to the whole EXCEPT construct, not the first sub-select as I suspect you're thinking. In short: WITH has lower syntactic precedence than UNION/INTERSECT/EXCEPT. You need parens if you want it to work the other way 'round. regards, tom lane