Course Content
Introduction to SQL
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:
SELECT continent FROM country ORDER BY continent DESC;
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.
SELECT id, name, region FROM country ORDER BY region, name DESC;
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:
id | name | continent | region | surfacearea | capital | population |
1 | Japan | Asia | Eastern Asia | 377829 | Tokyo | 126714000 |
2 | Latvia | Europe | NULL | 64589 | Riga | 2424200 |
3 | Mexico | North America | Central America | 1958201 | Mexico City | 98881000 |
... | ... | ... | ... | ... | ... | ... |
15 | Malta | Europe | Southern Europe | 316 | Valletta | 380200 |
Thanks for your feedback!
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:
SELECT continent FROM country ORDER BY continent DESC;
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.
SELECT id, name, region FROM country ORDER BY region, name DESC;
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:
id | name | continent | region | surfacearea | capital | population |
1 | Japan | Asia | Eastern Asia | 377829 | Tokyo | 126714000 |
2 | Latvia | Europe | NULL | 64589 | Riga | 2424200 |
3 | Mexico | North America | Central America | 1958201 | Mexico City | 98881000 |
... | ... | ... | ... | ... | ... | ... |
15 | Malta | Europe | Southern Europe | 316 | Valletta | 380200 |
Thanks for your feedback!
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:
SELECT continent FROM country ORDER BY continent DESC;
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.
SELECT id, name, region FROM country ORDER BY region, name DESC;
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:
id | name | continent | region | surfacearea | capital | population |
1 | Japan | Asia | Eastern Asia | 377829 | Tokyo | 126714000 |
2 | Latvia | Europe | NULL | 64589 | Riga | 2424200 |
3 | Mexico | North America | Central America | 1958201 | Mexico City | 98881000 |
... | ... | ... | ... | ... | ... | ... |
15 | Malta | Europe | Southern Europe | 316 | Valletta | 380200 |
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:
SELECT continent FROM country ORDER BY continent DESC;
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.
SELECT id, name, region FROM country ORDER BY region, name DESC;
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:
id | name | continent | region | surfacearea | capital | population |
1 | Japan | Asia | Eastern Asia | 377829 | Tokyo | 126714000 |
2 | Latvia | Europe | NULL | 64589 | Riga | 2424200 |
3 | Mexico | North America | Central America | 1958201 | Mexico City | 98881000 |
... | ... | ... | ... | ... | ... | ... |
15 | Malta | Europe | Southern Europe | 316 | Valletta | 380200 |