SELECT id, ((TO_DAYS(dt2)) - (TO_DAYS(dt1))) as diff FROM test order by ((TO_DAYS(dt2)) - (TO_DAYS(dt1)))