← 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
Ready to go further?

These free tutorials are a preview, our Backend Development cohort covers it in depth, with a real project and a cohort alongside you. Active

Enroll in Backend Development
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account