A second column breaks the ties
Sort by rating and several cafés can share the same rating — SQL has to put them in some order. Name a second column after a comma and it decides only those ties. DESC applies just to the column it follows, so the first column can be descending while the second stays ascending.
A tiny example
In plain English: Show me every café’s name and rating, highest rating first, and sort ties by name.
SELECT name, rating
FROM cafes
ORDER BY rating DESC, name;Query result
| name | rating |
|---|---|
| Corner Bean | 4.8 |
| Morning Mug | 4.8 |
| Sunny Cup | 4.6 |
Corner Bean and Morning Mug tie at 4.8, so the second column settles it by name, A before Z.
Common questions
- Which column sorts first in ORDER BY a, b?
- The first one listed. Rows are sorted by a, and only rows tied on a are then sorted by b. Each column can have its own ASC or DESC.
- Can I mix sort directions, like ORDER BY city ASC, total DESC?
- Yes. Each column gets its own direction, so city sorts A to Z while totals run largest to smallest within each city.
Practice ORDER BY with multiple columns step by step
The Foundations Track has beginner puzzles built around this idea. Tap tiles, see real results. No terminal, no setup.
practice this in the courseReady for a break?
A fresh, bite-sized SQL puzzle waits every morning. Tap tiles, see real results. No terminal, no setup, no timers.
Try today's free SQL puzzle