How do I find the largest value in a column in postgres sql?

后端 未结 4 1644
南方客
南方客 2021-02-03 17:44

For example:

name | weight
jon    100    
jane   120    
joe    130

How do I only return the name of the person with the largest weight?

4条回答
  •  旧巷少年郎
    2021-02-03 18:28

    ORDER BY DESC puts rows with null values at the top.

    To avoid returning results corresponding to null values:

    SELECT name FROM tbl WHERE weight = (SELECT MAX(weight) FROM tbl);

    Note: This query will return multiple results if multiple people have a weight equal to the maximum weight. To grab just one, add LIMIT 1 to the end of the query.


    Acknowledgements and more information:

    Why do NULL values come first when ordering DESC in a PostgreSQL query?

    MIN/MAX vs ORDER BY and LIMIT

    Postgres MAX Function

提交回复
热议问题