How to sort table values if there are NULL values present?

{ Creative Brackets }
{ Creative Brackets }
16/07/2020   /   0

In case you do ascend sort in the column which contains numerical values in MySQL, the result of sorted values might not be the one you were expecting if that column contains one or more NULL values.

Let's say you have a column named sort whose values are set to 0, 1, 2, 3, etc. If you sort the table with ORBER BY sort ASC, you probably expect an ascending string as a result.

What often happens is that values are: NULL, NULL, 0, 1, 2, 3, etc. This means that all NULL values have an advantage as compared to integer values. This is correct because MySQL considers NULL values lower than 0.

If you wish to place NULL values after integer values, your request must be written like this:

ORDER BY -sort DESC