How do I select an entire row which has the largest ID in the table?

Victor Kmita picture Victor Kmita · Sep 30, 2011 · Viewed 493.7k times · Source

How would I do something like this?

SQL SELECT row FROM table WHERE id=max(id)

Answer

unutbu picture unutbu · Sep 30, 2011

You could use a subselect:

SELECT row 
FROM table 
WHERE id=(
    SELECT max(id) FROM table
    )

Note that if the value of max(id) is not unique, multiple rows are returned.

If you only want one such row, use @MichaelMior's answer,

SELECT row from table ORDER BY id DESC LIMIT 1