mysql order by, null first, and DESC after

MysqlSql Order-By

Mysql Problem Overview


How can I order DESC by a field, but list the NULL values first?

So I'm having a table:

reuestId | offerId | offerTitle
1        | 1       | Alfa
NULL     | 2       | Beta
2        | 3       | Gamma

I want to select them so that the results would be:

NULL | 2 | Beta
2    | 3 | Gamma
1    | 1 | Alfa

Mysql Solutions


Solution 1 - Mysql

Try this:

ORDER BY [reuestId] IS NULL DESC, [reuestId] DESC

should work (for mySql)

Solution 2 - Mysql

SELECT *
FROM TableX
ORDER BY (requestId IS NOT NULL)
       , requestId DESC

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErvinView Question on Stackoverflow
Solution 1 - MysqlDonCallistoView Answer on Stackoverflow
Solution 2 - MysqlypercubeᵀᴹView Answer on Stackoverflow