Greater than or equal to ALL() and equal to MAX() speed

后端 未结 4 1462
春和景丽
春和景丽 2021-01-16 03:08

I have a relation in PostgreSQL named product which contains 2 fields: id and quantity, and I want to find the id of the

4条回答
  •  天涯浪人
    2021-01-16 03:41

    there is the 3rd variant

    SELECT id FROM product 
    WHERE quantity = (SELECT quantity FROM product ORDER BY quantity DESC NULLS LAST LIMIT 1)
    

    if the table has btree index as (quantity DESC NULLS LAST) this variant will be super-fast

提交回复
热议问题