Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite select max query and sorting

Tags:

sql

sqlite

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.

like image 824
im7xs Avatar asked Oct 09 '13 09:10

im7xs


1 Answers

select * 
from your_table
where product_price = (SELECT max(product_price) FROM your_table)
order by id desc
limit 1
like image 76
juergen d Avatar answered Oct 02 '22 07:10

juergen d