mysql order by desc null last 1

mysql order by desc null last

SELECT * FROM user 

ORDER BY COALESCE(name, 'zz') DESC

Here is what the above code is Doing:
1. The COALESCE function returns the first non-null value in a list.
2. The list is the name column and the string ‘zz’.
3. The string ‘zz’ is used as a placeholder for null values.
4. The ORDER BY clause sorts the rows in descending order.
5. The rows with null values in the name column are sorted last.

Similar Posts