Is there a way with PostgreSQL to sort rows with NULL
values in fields to the end of the selected table?
Like:
SELECT * FROM table ORDER BY somevalue, PUT_NULL_TO_END
Is there a way with PostgreSQL to sort rows with NULL
values in fields to the end of the selected table?
Like:
SELECT * FROM table ORDER BY somevalue, PUT_NULL_TO_END
Does this make the trick?
Taken from: http://www.postgresql.org/docs/9.0/static/sql-select.html
First of all, 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 first. The solution @Mosty pointed out was introduced with PostgreSQL 8.3:
For PostgreSQL 8.2 and older or other RDBMS without this standard SQL feature you can substitute:
FALSE
sorts beforeTRUE
, so NULL values come last, just like in the example above.Related later answer: