Sorting by Multiple Columns
You can sort by multiple columns. The first column is the primary sort, and subsequent columns break ties.
Multi-Column Sort Syntax
SELECT * FROM users ORDER BY column1, column2;
Each column can have its own ASC/DESC direction:
SELECT * FROM users ORDER BY city ASC, age DESC;
Exercises
Loading exercise...
Loading exercise...
Loading exercise...
Why Multi-Column Sorting Matters
Consider this scenario: you have multiple users in the same city. Without a secondary sort, their order within that city is undefined. Adding a second sort column ensures consistent, predictable results.
Loading exercise...
Free Practice
Loading SQL editor...
Try:
- Sort by age, then city
- Sort by city descending, then name ascending
- Add a filter and multi-column sort

