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

ORDER BY with Multiple Columns

This lesson covers sorting by multiple columns, so results can follow a two-level (or deeper) priority order. It's for anyone searching "SQL ORDER BY multiple columns."

Specifying ORDER BY col1, col2 sorts by more than one condition in sequence — like "by class first, then by score within each class."

The example uses ORDER BY class_id ASC, score DESC: first ascending by class ID, then — within matching class IDs — descending by score. The rule: whenever the first column ties, the second column breaks the tie.

A common early mistake is not realizing each column can have its own independent ASC/DESC — you can mix ascending on one column with descending on another within the same ORDER BY, exactly like this example does. This kind of multi-level sort — say, for a roster or directory — comes up constantly in real reporting, and each column's direction can be tuned independently.

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)