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 1v8gff-001Uoc-95 for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Oct 2025 15:10:38 +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 1v8gfd-0013mv-KL for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Oct 2025 15:10:36 +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 1v8gfd-0013mn-Bs for pgsql-bugs@lists.postgresql.org; Tue, 14 Oct 2025 15:10:36 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v8gfa-001jbl-2X for pgsql-bugs@lists.postgresql.org; Tue, 14 Oct 2025 15:10:35 +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 59EFAVM3959908; Tue, 14 Oct 2025 11:10:31 -0400 From: Tom Lane To: depesz@depesz.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: order by alias - doesn't work sometimes? In-reply-to: References: Comments: In-reply-to hubert depesz lubaczewski message dated "Tue, 14 Oct 2025 16:50:56 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <959906.1760454631.1@sss.pgh.pa.us> Date: Tue, 14 Oct 2025 11:10:31 -0400 Message-ID: <959907.1760454631@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk hubert depesz lubaczewski writes: > But: > =$ select unnest( ARRAY['d', 'c', 'a'] ) x order by x <> 'a'; > ERROR: column "x" does not exist > LINE 1: select unnest( ARRAY['d', 'c', 'a'] ) x order by x <> 'a'; > ^ > Obviously I can put the unnest() call in from, and then it works, but > I'm curious, why we can `order by x`, but not `order by x <> 'a'` ? ORDER BY output-column-alias is a messy hangover from SQL92. In later SQL versions, identifiers in ORDER BY have the same interpretation as in WHERE and other clauses, namely they are columns sourced by the FROM clause. Supporting both interpretations makes for a lot of ambiguity, so we only allow the old interpretation in exactly the case required by SQL92, namely "ORDER BY identifier". This is documented ... regards, tom lane