SQLite: Selecting the maximum corresponding value

20,973

Solution 1

Get the records with the largest IDs first, then stop after the first record:

SELECT * FROM MyTable ORDER BY id DESC LIMIT 1

Solution 2

Just like the mysql, you can use MAX()

e.g. SELECT MAX(id) AS member_id, name, value FROM YOUR_TABLE_NAME

Solution 3

If you want to know the query syntax :

String query = "SELECT MAX(id) AS max_id FROM mytable";
Share:
20,973
abidinberkay
Author by

abidinberkay

Updated on April 28, 2020

Comments

  • abidinberkay
    abidinberkay about 4 years

    I have a table with three columns as follows:

    id INTEGER    name TEXT    value REAL
    

    How can I select the value at the maximum id?

  • abidinberkay
    abidinberkay almost 11 years
    I want to select the value not id.
  • Michael
    Michael almost 9 years
    What if you want to select on the row that has the maximum of two different cells?
  • Ankur Soni
    Ankur Soni over 7 years
    Above Query Searches n records, then arranges n records, then gets 1 record...?????? is this query efficient, it needs tuning. Below answer by @Newbie is much better in terms of efficiency.
  • Wirsing
    Wirsing over 7 years
    In a LIMIT n query, SQLite keeps only the n largest rows while sorting. And if the column is indexed, no sorting is necessary.
  • Richard-Degenne
    Richard-Degenne about 7 years
    == doesn't exists in SQL, if I'm not mistaken.