Need a postgresql query which return null value at the end of the non-null value irrespective of the Ascending/Descending Order.
For ex:- I have a column say purchase_price having values 1, 5, 3, 0, null, null
Then for ORDER BY purchase_price ASC It should return
0
1
3
5
null
null
and for ORDER BY purchase_price DESC It should return
5
3
1
0
null
null
I need generalise solution which i can able to apply for the 'boolean', 'float', 'string', 'integer', 'date' Datatype
order bysupportsnulls first/last. By defaultascgetsnulls lastanddescgetsnulls first. You can override them for each column:An extra note on this: the underlying reason is that nulls they get placed at the end of the btree index. In PostgreSQL 9.0,
order by col desc nulls lastis optimized to split the index scan in two (reverse scan on not null rows, followed by null rows). This was not the case in the original version where it was added (8.4 if memory serves); the latter proceeds to do a seq scan of the whole table followed by a quicksort). And older versions do not support the feature at all. So be wary of using it in anything but recent versions of PostgreSQL.