Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 8DEA14766D3 for ; Tue, 26 Nov 2002 10:30:00 -0500 (EST) Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85]) by postgresql.org (Postfix) with ESMTP id 97F014766C7 for ; Tue, 26 Nov 2002 10:29:59 -0500 (EST) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-35.mail.demon.net with esmtp (Exim 3.36 #2) id 18Ghf1-0007n4-0U; Tue, 26 Nov 2002 15:30:03 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id EF01F17508; Tue, 26 Nov 2002 15:26:40 +0000 (GMT) Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id 360F617506; Tue, 26 Nov 2002 15:26:40 +0000 (GMT) Content-Type: text/plain; charset="utf-8" From: Richard Huxton Organization: Archonet Ltd To: "Zuev Dmitry" , pgsql-sql@postgresql.org Subject: Re: help on sql query Date: Tue, 26 Nov 2002 15:26:37 +0000 User-Agent: KMail/1.4.3 References: In-Reply-To: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200211261526.37984.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/391 X-Sequence-Number: 10533 On Tuesday 26 Nov 2002 1:14 pm, Zuev Dmitry wrote: > Suppose you have a table T: > > A B > 1 '111' > 2 '222' > > How do you select A and B of the record with A maximum? > 1) SELECT A, B FROM T ORDER BY A DESC LIMIT 1 > > 2) SELECT A, B FROM T WHERE A IN (SELECT MAX(A) FROM T) > > What do yo think of the queries above? And give me the better > implementation if possible. If you have an index on A the first option will be faster. All PG has to do= in=20 this case is check the end of the index and fetch one row. --=20 Richard Huxton