Обсуждение: help on sql query

Поиск
Список
Период
Сортировка

help on sql query

От
"Zuev Dmitry"
Дата:
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?

The desirable result therefore is (2, '222')

The two variants I could grow with are:

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.

__________________________________________________________________ Dmitry
Vitalievitch ICQ#: 11000008 Current ICQ status: + More ways to contact me
__________________________________________________________________




Re: help on sql query

От
Richard Huxton
Дата:
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