Order NULLs last (numeric only)

Every now and then I need to sort data by a numeric value and NULLs always come first. And I want them last.

Let’s set up a table with some data in it:

I want to sort data by ord column:

which produces:

Now, i want to move NULLs to the end:

which produces:

This -ord DESC is basically multiplying ord by -1, hence reversing the order, and sort DESC will put them back in ASC order. DESC will also put the NULL values at the end, and we get the result we’re after.

Tweet about this on TwitterShare on LinkedInShare on FacebookShare on Google+

Leave a Comment