ASCending DESCending
suggest changeORDER BY x ASC -- same as default
ORDER BY x DESC -- highest to lowest
ORDER BY lastname, firstname -- typical name sorting; using two columns
ORDER BY submit_date DESC -- latest first
ORDER BY submit_date DESC, id ASC -- latest first, but fully specifying order.
ASCmeans ascending andDESCmeans descending. MySQL does not acceptASCENDINGorDESCENDINGas keywords.NULLs sort first forASCand last forDESC. MySQL has noNULLS FIRSTorNULLS LASTclause.- In the above examples,
INDEX(x),INDEX(lastname, firstname), andINDEX(submit_date)may significantly improve performance.
Mixing ASC and DESC, as in the last example, cannot use a composite index before MySQL 8.0. From MySQL 8.0, an index declared with the same directions, such as INDEX(submit_date DESC, id ASC), can be used. Before 8.0, DESC in an index declaration was accepted and ignored.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents