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";
Author by
abidinberkay
Updated on April 28, 2020Comments
-
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 maximumid
? -
abidinberkay almost 11 yearsI want to select the value not id.
-
Michael almost 9 yearsWhat if you want to select on the row that has the maximum of two different cells?
-
Ankur Soni over 7 yearsAbove 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 over 7 yearsIn 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 about 7 years
==
doesn't exists in SQL, if I'm not mistaken.