1

I have a table, with these columns:

ID  |  Data

How to find out which record has highest ID?

4 Answers 4

9

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)
Sign up to request clarification or add additional context in comments.

Comments

2
select *
    from YourTable
    where ID = (select max(ID) from YourTable)

Comments

2
select max(ID) from tablename

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.