Search code examples
sqlvb.netoledb

How can I identify which record has the MAX for a particular field?


The title is pretty much self explanatory. I want to find out a way to identify which record the MAX of a field is in.


Solution

  • Try this with MySQL or Postgres:

    SELECT * FROM MyTable ORDER BY MyField DESC LIMIT 1
    

    Or this with MSSQL:

    SELECT TOP 1 * FROM MyTable ORDER BY MyField DESC
    

    Oracle requires a nested SELECT with ROWNUM:

    SELECT * FROM (SELECT * FROM MyTable ORDER BY MyField DESC) WHERE ROWNUM = 1