How to Order By Multiple Columns in SQL

Published Sep 21, 2021

How can we order by two or more columns in MySQL, or any other SQL database framework?

We can simply list out the columns after the ORDER BY.

ORDER BY column1, column2

This will sort everything by column1. If column1 is the same for two rows, then it’ll sort by column2.

Descending Order with Two Columns

We can also sort in descending order for any of the columns.

ORDER BY column1 DESC, column2

Example: Ordering by Two Columns

Suppose we have this table dogs.

dogval
corgi5
pug1
husky5
shih tzu10

Let’s execute the query below.

SELECT * FROM dogs
ORDER BY val DESC, dog ASC

The resulting set will look like this.

dogval
shih tzu10
corgi5
husky5
pug1

More SQL Articles