Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Specifying Sort Direction | Sorting Retrieved Data
Introduction to SQL

Specifying Sort Direction

We can sort data in more than just ascending order, which is the default sort order. The ORDER BY clause can sort the data in descending order, and to do this, we need to specify the keyword DESC. Let's look at an example:

Sorting Multiple Columns in Descending Order

We can sort the data by multiple columns. In fact, it is often necessary to sort data by more than one column. For instance, when displaying a list of students, we may want to display it sorted by last name and first name. This kind of sorting is useful when multiple students share the same name. When sorting in descending order on multiple columns, each column should have its own DESC keyword. Let's see an example.

Explanation: The DESC keyword must only be applied to the column's name we want to sort in descending order. In our example, DESC was specified for the name column but not for the region column. Therefore, the name column is sorted in descending order, but the region column is sorted in ascending order (ascending order is the default).

Task

Retrieve five columns using the SELECT statement, namely: name, continent, region, population, and capital (please retrieve these columns in this order). Sort the result by two of them: first by continent in descending order, then by population in ascending order.

Here's a short example of the country table:

idnamecontinentregionsurfaceareacapitalpopulation
1JapanAsiaEastern Asia377829Tokyo126714000
2LatviaEuropeNULL64589Riga2424200
3MexicoNorth AmericaCentral America1958201Mexico City98881000
.....................
15MaltaEuropeSouthern Europe316Valletta380200

Everything was clear?

Section 2. Chapter 5
toggle bottom row
course content

Course Content

Introduction to SQL

Specifying Sort Direction

We can sort data in more than just ascending order, which is the default sort order. The ORDER BY clause can sort the data in descending order, and to do this, we need to specify the keyword DESC. Let's look at an example:

Sorting Multiple Columns in Descending Order

We can sort the data by multiple columns. In fact, it is often necessary to sort data by more than one column. For instance, when displaying a list of students, we may want to display it sorted by last name and first name. This kind of sorting is useful when multiple students share the same name. When sorting in descending order on multiple columns, each column should have its own DESC keyword. Let's see an example.

Explanation: The DESC keyword must only be applied to the column's name we want to sort in descending order. In our example, DESC was specified for the name column but not for the region column. Therefore, the name column is sorted in descending order, but the region column is sorted in ascending order (ascending order is the default).

Task

Retrieve five columns using the SELECT statement, namely: name, continent, region, population, and capital (please retrieve these columns in this order). Sort the result by two of them: first by continent in descending order, then by population in ascending order.

Here's a short example of the country table:

idnamecontinentregionsurfaceareacapitalpopulation
1JapanAsiaEastern Asia377829Tokyo126714000
2LatviaEuropeNULL64589Riga2424200
3MexicoNorth AmericaCentral America1958201Mexico City98881000
.....................
15MaltaEuropeSouthern Europe316Valletta380200

Everything was clear?

Section 2. Chapter 5
toggle bottom row
some-alt