RSS

NULLS First | Last

09 Jan

I thought to write this blog because I found a fantastic and very helpful feature with ORDER BY clause.

Actually I have data which I want to sort by using the Order by clause, but unfortunately the data has lot of NULL values too. And you know it doesn’t look good if we sort values in DESC order and all the null values got displayed on top (by default). While I wish all the null value records should display at last. And as usual as a lazy programmer I don’t want to handle the issue with code.

So what, I did my favorite – Goooooooooogling, and I found a helpful information to share.

We have NULLS First | Last keywords with Order by clause to specify that NULL values should be returned Before | After non-null values. For example:

SELECT * FROM users ORDER BY lastname DESC NULLS LAST;

It make my job done perfectly! Hope this helps you all.

 
Leave a comment

Posted by on January 9, 2014 in MySQL

 

Tags: , , , , , , ,

Leave a comment