im7xs im7xs - 3 months ago 22
SQL Question

Sqlite select max query and sorting

I have the following fields on a SQLite table,

ID - PRODUCT_NAME - PRODUCT_PRICE
1 WCC9899 1023
2 WCC9399 9999
3 WCC93W9 344
4 WCC9819 55
5 WCC3333 1023


This query returns the first item with the highest price:

SELECT max(product_price) as price, product_name, id FROM table


But want to get the item with the highest price ordered by the last id, in this case the result should be:

5 WCC3333 1023


Instead i get:

1 WCC9899 1023


Order by doesn't works, ty.

Answer
select * 
from your_table
where product_price = (SELECT max(product_price) FROM your_table)
order by id desc
limit 1