views:

72

answers:

4
+1  Q: 

Simple SQL query

I have a table, with these columns:

ID  |  Data

How to find out which record has highest ID?

+2  A: 
select max(ID) from tablename
jamietre
+9  A: 

To get the largest ID:

select max(ID) from myTable

To get a record that has the largest ID:

select *
from MyTable
where ID = (Select max(ID) from myTable)
FrustratedWithFormsDesigner
+2  A: 
select *
    from YourTable
    where ID = (select max(ID) from YourTable)
Joe Stefanelli
+1  A: 

As well as max, you can use TOP on SQL Server

select TOP 1 * from myTable order by id desc

For joint top

select TOP 1 WITH TIES * from myTable order by id desc

Other engines have LIMIT not TOP. This can give the whol record without a separate MAX sub-query too

gbn
Until the ANSI:2008 `FETCH FIRST 1 ROWS ONLY` gets better support...
OMG Ponies