How to Order By Multiple Columns in SQL

Published Sep 21, 2021  ∙  Updated May 2, 2022

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.

dog val
corgi 5
pug 1
husky 5
shih tzu 10

Let’s execute the query below.

SELECT * FROM dogs
ORDER BY val DESC, dog ASC

The resulting set will look like this.

dog val
shih tzu 10
corgi 5
husky 5
pug 1