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 1qHRbS-0007ST-9N for pgsql-sql@arkaria.postgresql.org; Thu, 06 Jul 2023 16:13:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qHRbR-0006a5-69 for pgsql-sql@arkaria.postgresql.org; Thu, 06 Jul 2023 16:13:09 +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 1qHRaP-0002kH-Dj for pgsql-sql@lists.postgresql.org; Thu, 06 Jul 2023 16:12:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qHRaN-002Nri-7X for pgsql-sql@lists.postgresql.org; Thu, 06 Jul 2023 16:12:04 +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 366GC1J41218124; Thu, 6 Jul 2023 12:12:01 -0400 From: Tom Lane To: Shaozhong SHI cc: "David G. Johnston" , pgsql-sql Subject: Re: How to select the last value/row? In-reply-to: References: Comments: In-reply-to Shaozhong SHI message dated "Thu, 06 Jul 2023 17:02:40 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1218122.1688659921.1@sss.pgh.pa.us> Date: Thu, 06 Jul 2023 12:12:01 -0400 Message-ID: <1218123.1688659921@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Shaozhong SHI writes: > How about > ID > 5 > 4 > 3 > 3 > 2 > 3 > The last is 3. You need to reorient your thinking. In SQL, row sets are unordered sets of values --- this is not a Postgres deficiency, it's a fundamental tenet of the relational data model. If you want some kind of ordering, you have to express that by an ORDER BY clause, which means you need something within the data that corresponds to what you want the ordering to be. Your example above is basically nonsense from the standpoint of SQL. regards, tom lane