Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oqjl7-0003uu-EZ for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Nov 2022 23:36:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oqjl6-000843-Br for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Nov 2022 23:36:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oqjl6-00083u-3V for pgsql-bugs@lists.postgresql.org; Thu, 03 Nov 2022 23:36:28 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oqjl3-00039O-SC for pgsql-bugs@postgresql.org; Thu, 03 Nov 2022 23:36:27 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 2A3NaKTS2193246; Thu, 3 Nov 2022 19:36:20 -0400 From: Tom Lane To: Vik Fearing cc: pgsql-bugs Subject: Re: Wrong result with constant quals In-reply-to: References: Comments: In-reply-to Vik Fearing message dated "Fri, 04 Nov 2022 00:19:16 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2193244.1667518580.1@sss.pgh.pa.us> Date: Thu, 03 Nov 2022 19:36:20 -0400 Message-ID: <2193245.1667518580@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Vik Fearing writes: > The following query returns a wrong result, in my opinion. > postgres=# select 1 where false having true; > ?column? > ---------- > 1 > (1 row) > The correct result should be zero rows. No, I don't think so. The presence of HAVING without GROUP BY makes this act like a query with an aggregate function and no GROUP BY: you get a single grouped row, regardless of what the input is. There's a reasonably clear specification of that in SQL92 7.8 : 1) Let T be the result of the preceding , , or . If that clause is not a , then T consists of a single group and does not have a grouping column. "A single group" is not "no groups". Later SQL versions define this by reference to "GROUP BY ()", but I think the effect is the same. regards, tom lane