im7xs im7xs - 1 year ago 159
SQL Question

Sqlite select max query and sorting

I have the following fields on a SQLite table,

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 Source
select * 
from your_table
where product_price = (SELECT max(product_price) FROM your_table)
order by id desc
limit 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download