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
course content

Course Content

Introduction to SQL

Introduction to SQL

1. Retrieving Data
2. Sorting Retrieved Data
3. Filtering Data
4. Advanced Data Filtering
5. Aggregate Functions

bookSpecifying 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:

123
SELECT continent FROM country ORDER BY continent DESC;
copy

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.

123
SELECT id, name, region FROM country ORDER BY region, name DESC;
copy

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
toggle bottom row

bookSpecifying 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:

123
SELECT continent FROM country ORDER BY continent DESC;
copy

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.

123
SELECT id, name, region FROM country ORDER BY region, name DESC;
copy

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
toggle bottom row

bookSpecifying 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:

123
SELECT continent FROM country ORDER BY continent DESC;
copy

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.

123
SELECT id, name, region FROM country ORDER BY region, name DESC;
copy

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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:

123
SELECT continent FROM country ORDER BY continent DESC;
copy

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.

123
SELECT id, name, region FROM country ORDER BY region, name DESC;
copy

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt