Received: from postgresql.org.org (webmail.postgresql.org [216.126.85.28]) by postgresql.org (8.11.3/8.11.1) with SMTP id f5CGXME41281 for ; Tue, 12 Jun 2001 12:33:22 -0400 (EDT) (envelope-from pgsql-sql-owner@postgresql.org) Received: from coldresist.com (cr271446-a.rchmd1.bc.wave.home.com [24.113.128.171]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f576uNE17166 for ; Thu, 7 Jun 2001 02:56:23 -0400 (EDT) (envelope-from pozix@home.com) Received: from idyllic.home.com ([192.168.0.7]) by coldresist.com (8.11.2/8.11.2) with ESMTP id f576wkx17857 for ; Wed, 6 Jun 2001 23:59:27 -0700 Message-Id: <5.1.0.14.0.20010606231409.02b92168@mail.rchmd1.bc.wave.home.com> X-Sender: gerald@mail.rchmd1.bc.wave.home.com X-Mailer: QUALCOMM Windows Eudora Version 5.1 Date: Wed, 06 Jun 2001 23:20:28 -0700 To: pgsql-sql@postgresql.org From: Gerald Gutierrez Subject: Getting row with id=max(id) Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Archive-Number: 200106/179 X-Sequence-Number: 3412 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 /really/ long time on a table that is large. The query: SELECT * FROM mytable ORDER BY id DESC LIMIT 1; doesn't seem to help very much. What query is the fastest at getting this row? A related question is: is there a way to time a query in psql, like the client of MySQL does?