NULL values are sorted last in default ascending order. You don’t have to do anything extra.
The issue applies to descending order, which is the perfect inverse and thus sorts
NULL values on top.
PostgreSQL 8.3 introduced
ORDER BY somevalue DESC NULLS LAST
For PostgreSQL 8.2 and older or other RDBMS without this standard SQL feature:
ORDER BY (somevalue IS NULL), somevalue DESC
FALSE sorts before
NULL values come last, just like in the example above.
- Sort by column ASC, but NULL values first?
- The manual on