[SQL] Getting row with id=max(id)

2001-06-07 Thread Gerald Gutierrez
I'd like to retrieve a row of a table that has the maximum ID. For example, with: id | s +--- 1 | alpha 2 | beta 3 | gamma 4 | delta I'd like to get the row with ID=4. I've tried: SELECT * FROM mytable WHERE id=(SELECT MAX(id) FROM mytable); The subquery can take a /r

[SQL] Getting row with id=max(id)

2001-06-12 Thread Gerald Gutierrez
I'd like to retrieve a row of a table that has the maximum ID. For example, with: id | s +--- 1 | alpha 2 | beta 3 | gamma 4 | delta I'd like to get the row with ID=4. I've tried: SELECT * FROM mytable WHERE id=(SELECT MAX(id) FROM mytable); The subquery can take a /r

Re: [SQL] Getting row with id=max(id)

2001-06-07 Thread Gerald Gutierrez
At 07:31 PM 6/7/2001 +0200, Peter Eisentraut wrote: > > SELECT * FROM mytable WHERE id=(SELECT MAX(id) FROM mytable); > > SELECT * FROM mytable ORDER BY id DESC LIMIT 1; >The second is generally thought to be faster, at least if you use the >latest version of PostgreSQL. This is quite amusing ac

Re: [SQL] Getting row with id=max(id)

2001-06-07 Thread Gerald Gutierrez
>=> explain select id from mytable order by seed desc limit 1; Oops, a cut & paste mistake. That should be: explain select id from mytable order by id desc limit 1; ---(end of broadcast)--- TIP 5: Have you checked our extensive FAQ? http://www.

Re: [SQL] Getting row with id=max(id)

2001-06-08 Thread Tom Lane
Gerald Gutierrez <[EMAIL PROTECTED]> writes: > I'd like to get the row with ID=4. I've tried: > SELECT * FROM mytable WHERE id=(SELECT MAX(id) FROM mytable); > The subquery can take a /really/ long time on a table that is large. The query: > SELECT * FROM mytable ORDER BY id DESC LIMIT 1; > doesn'

Re: [SQL] Getting row with id=max(id)

2001-06-12 Thread jeff
> A related question is: is there a way to time a query in psql, like the > client of MySQL does? use the explain commmand explain select * from foo; > > > ---(end of broadcast)--- > TIP 2: you can get off all lists at once with the unregister co