Re: help on sql query

From: Richard Huxton <dev(at)archonet(dot)com>
To: "Zuev Dmitry" <envoy1(at)chat(dot)ru>, pgsql-sql(at)postgresql(dot)org
Subject: Re: help on sql query
Date: 2002-11-26 15:26:37
Message-ID: 200211261526.37984.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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
this case is check the end of the index and fetch one row.

--
Richard Huxton

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Gulutzan 2002-11-26 15:30:12 Re: [GENERAL] Bug with sequence
Previous Message Richard Huxton 2002-11-26 15:24:05 Re: Two TIMESTAMPs in one pl/sql function