For example:
name | weight
jon 100
jane 120
joe 130
How do I only return the name of the person with the largest weight?
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