Ad space (banner)
🗄️SQL Lessons
Lesson 4 / 50

Sorting Results with ORDER BY

This lesson covers sorting query results with ORDER BY, so search results come back in a sensible order. It's for anyone searching "SQL ORDER BY tutorial."

ORDER BY column DESC sorts largest-to-smallest, and ASC sorts smallest-to-largest. ASC is the default when nothing is specified, so if you want descending order you have to say DESC explicitly.

The example uses ORDER BY score DESC to list students from highest score to lowest. Try switching it to ASC to see the order flip — that side-by-side comparison makes the difference stick.

A common early mistake is putting ORDER BY in the wrong position — SQL clauses have a fixed order (SELECT ... FROM ... WHERE ... ORDER BY ...), and breaking that order causes a syntax error. This clause is essential whenever you want to show a leaderboard, a "most recent orders" list, or any result a human needs to scan in a meaningful order — and it's frequently combined with WHERE.

SQL
OUTPUT

💡 The SQL engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)