agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Zuev Dmitry <envoy1@chat.ru>
To: pgsql-sql@postgresql.org
Subject: Re: help on sql query
Date: Tue, 26 Nov 2002 15:26:37 +0000
Message-ID: <200211261526.37984.dev@archonet.com> (raw)
In-Reply-To: <arvs7m$fpf$1@news.nsk.su>
References: <arvs7m$fpf$1@news.nsk.su>

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



view thread (7+ messages)  latest in thread

Message-ID: <200211261526.37984.dev@archonet.com>
Permalink:  ../200211261526.37984.dev@archonet.com/
Also on:    postgresql.org/message-id/200211261526.37984.dev@archonet.com

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, envoy1@chat.ru
  Subject: Re: help on sql query
  In-Reply-To: <200211261526.37984.dev@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox