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

Kursinnhold

Introduction to SQL

Introduction to SQL

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

book
Specifying Sort Direction

Sorting Columns in Descending Order

We can do more than just sort data in ascending order by default. The ORDER BY clause can also arrange data in descending order, which requires using the DESC keyword.

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 data by multiple columns, and in many cases, this is actually necessary. For example, when displaying a list of students, you might want to order them first by their last name and then by their first name. This approach is especially helpful when multiple students share the same name.

Also, if you’re sorting in descending order by more than one column, remember that each column must include its own DESC keyword.

Let's see an example.

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

Explanation: You only need to apply the DESC keyword to the column you want sorted in descending order. In our example, we used DESC for the name column but not for the region column. As a result, the name column is sorted in descending order, while the region column remains sorted in ascending order (the default).

Here is the country table we are working with:

Oppgave

Swipe to start coding

Write an SQL query to 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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 5
toggle bottom row

book
Specifying Sort Direction

Sorting Columns in Descending Order

We can do more than just sort data in ascending order by default. The ORDER BY clause can also arrange data in descending order, which requires using the DESC keyword.

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 data by multiple columns, and in many cases, this is actually necessary. For example, when displaying a list of students, you might want to order them first by their last name and then by their first name. This approach is especially helpful when multiple students share the same name.

Also, if you’re sorting in descending order by more than one column, remember that each column must include its own DESC keyword.

Let's see an example.

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

Explanation: You only need to apply the DESC keyword to the column you want sorted in descending order. In our example, we used DESC for the name column but not for the region column. As a result, the name column is sorted in descending order, while the region column remains sorted in ascending order (the default).

Here is the country table we are working with:

Oppgave

Swipe to start coding

Write an SQL query to 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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 5
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt