Sorting by Several Columns
ORDER BY can take more than one column. SQL sorts by the first one, then uses the next to break ties.
Everyone grouped by department, highest paid first inside each one:
Each column gets its own direction. Here department_id sorts ascending (the default) and salary sorts descending.
Ties matter more than you'd think. Three engineers were hired on 2020-03-15, so their order here isn't guaranteed:
Add a second column and the order becomes predictable:
You can also sort by an alias from SELECT:
Check your understanding
In ORDER BY department_id, salary DESC, what does salary DESC do?
Examples run on the employees sample database. Open any of them in the playground to experiment.