← SQL tutorial

SQL

ORDER BY

ORDER BY column sorts the results. Add DESC for descending order (highest/latest first).

Example: highest score first

SELECT name, score FROM students ORDER BY score DESC;
name  | score
Ada   | 92
Musa  | 85
Chidi | 78

Without ORDER BY at all, a database makes no promise about what order rows come back in — it might happen to match insertion order, or it might not, especially once a table has been updated many times. Never rely on "the order it happens to return" without an explicit ORDER BY; the default without it is ascending order (ASC), which is why you only need to write DESC explicitly, never ASC.

Example
SELECT name, score FROM students ORDER BY score DESC;
Output
name  | score
Ada   | 92
Musa  | 85
Chidi | 78