Handle Null value in Order by clause in Mysql

select Null value in upper or lower in Order by clause in MySQL

Null value occurred at the last position

select rental_id,return_Date from rental order by return_date is null, return_Date desc 

rental_id|return_Date        |
---------+-------------------+
    16005|2005-09-02 02:35:22|
    16040|2005-09-02 02:19:33|
    15971|2005-09-02 01:28:33|
    15928|2005-09-01 23:43:24|
    15922|2005-09-01 22:27:31|
    15977|2005-09-01 22:12:10|
    15982|2005-09-01 21:51:31|
    15973|2005-09-01 20:08:41|
    15834|2005-09-01 19:31:50|
    15911|2005-09-01 19:14:53|

Null value present at the upper position

select rental_id,return_Date from rental order by return_date is not null, return_Date desc;

rental_id|return_Date        |
---------+-------------------+
    14734|                   |
    14741|                   |
    14760|                   |
    14769|                   |
    14878|                   |
    14915|                   |
    14928|                   |
    14933|                   |

Default Null value at the first position in Mysql

select * from rental order by return_Date asc

rental_id|return_Date        |
---------+-------------------+
    11496|                   |
    11541|                   |
    11563|                   |
    11577|                   |
    11593|                   |
    11611|                   |
    11646|                   |

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.