Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Sorting by Multiple Columns | Sorting Retrieved Data
Introduction to SQL (60/40)

bookSorting by Multiple Columns

We can also sort by multiple columns. We need to specify the column names separated by commas.

The first column specified in the sorting will have priority, and then the sorting will proceed according to the second column.

The syntax will look like this:

SELECT columns 
FROM table 
ORDER BY column_with_priority, column2, ...

Let’s look at the example that retrieves three columns and sorts the results by two of them: first by population and then by capital:

123
SELECT ID, population, capital FROM country ORDER BY population, capital;
copy
question mark

Which SQL clause is used to sort query results by multiple columns?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what happens if two countries have the same population?

What other columns can I sort by in this table?

Can I sort in descending order instead of ascending?

bookSorting by Multiple Columns

Swipe to show menu

We can also sort by multiple columns. We need to specify the column names separated by commas.

The first column specified in the sorting will have priority, and then the sorting will proceed according to the second column.

The syntax will look like this:

SELECT columns 
FROM table 
ORDER BY column_with_priority, column2, ...

Let’s look at the example that retrieves three columns and sorts the results by two of them: first by population and then by capital:

123
SELECT ID, population, capital FROM country ORDER BY population, capital;
copy
question mark

Which SQL clause is used to sort query results by multiple columns?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
some-alt