Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the row with maximum/minimum value in SQLite

According to the docs, common sense and some manuals on common SQL the max function returns only maximum value. So the correct way to select the row(s) with the maximum value is a subquery:

select * from `table` where `a`=(select max(`a`) from `table`);

It's inefficient. Are there something like argmax in SQLite?

like image 490
KOLANICH Avatar asked Jan 04 '23 18:01

KOLANICH


2 Answers

According to the docs, SQLite allows to select the entire row with max():

SELECT *, max(a) FROM MyTable;

(This is supported since version 3.7.11.)

like image 117
CL. Avatar answered Jan 08 '23 07:01

CL.


It is. Just use

select max(`a`), * from `table`;

if a single row is enough for you. The rest of values will belong to the row from which the maximum value is taken.

like image 30
KOLANICH Avatar answered Jan 08 '23 05:01

KOLANICH