LESSON 12 / 62 · SortingSorting, ORDER BY operator
SQL ORDER BY operator, sorting by multiple columns, examples of usage
When executing a SELECT query, the rows are returned by default in an undefined order. In fact, the order of rows in this case depends on the connection and scanning plan, as well as the order of data placement on the disk, so it cannot be relied upon. The ORDER BY construct is used to order the records.
General query structure with ORDER BY operator
SELECT table_fields FROM table_name
[WHERE condition]
ORDER BY column [ASC | DESC] [, ...];- [ ]optional part
- a | bone of the options
- ...can be repeated
Which rows to keep before sorting.
The column whose values define the order of rows. You can list several columns separated by commas.
Sorting direction: ASC for ascending (the default), DESC for descending.
- table_fields
- The columns to include in the result.
- table_name
- The table to select data from.
- condition
- Optional
- ASC | DESC
- KeywordOptionalASCDESC
For example, let's display the names of airlines in alphabetical order from the Company table:
SELECT name FROM Company ORDER BY name;| name |
|---|
| Aeroflot |
| Air_France |
| British_AW |
| Dale_avia |
| Don_avia |
Sorting in ascending and descending order for main types
| Data type | ASC | DESC |
|---|---|---|
| String type | Lexicographic (alphabetical) order from "A" to "Z" Records starting with "a" come first, then "b", etc. | Lexicographic order from "Z" to "A" Records starting with "z" come first, then "y", etc. |
| Numeric type | From smallest to largest | From largest to smallest |
| Date and time | From earlier date/time to later For example, first 01.01.2024, then 01.02.2024 | From later date/time to earlier For example, first 01.02.2024, then 01.01.2024 |
| Boolean type | false comes before true | true comes before false |
| NULL values | NULL values come first | NULL values come last |
Sorting by multiple columns
To sort results by two or more columns, they should be specified separated by commas.
MySQL 8.4
ORDER BY column_1 [ASC | DESC], column_2 [ASC | DESC];- [ ]optional part
- a | bone of the options
The column used to sort rows that have the same value in column_1.
Sorting direction for the column it follows. The default is ASC.
- column_1
- The column rows are sorted by first.
- ASC | DESC
- KeywordOptionalASCDESC
Data will be sorted by the first column, but in case there are multiple records with identical values in the first column, they will be sorted by the second column. The number of columns that can be sorted is unlimited.
The sorting rule applies only to the column that follows it.
ORDER BY column_1, column_2 DESC
is not the same as
ORDER BY column_1 DESC, column_2 DESC
Let's display flight information sorted by the departure city in ascending order and by the arrival city in descending order, from the Trip table:
SELECT DISTINCT town_from, town_to FROM Trip
ORDER BY town_from, town_to DESC;| town_from | town_to |
|---|---|
| London | Singapore |
| London | Paris |
| Moscow | Rostov |
| Paris | Rostov |
| Rostov | Vladivostok |
| Rostov | Paris |
| Rostov | Moscow |
| Singapore | London |
| Vladivostok | Rostov |
In this example, the entries are sorted by the town_from field first. Then, it performs reverse sorting by the town_to field for groups of rows that have the same value in the town_from column.
Demonstration of how sorting works
SELECT * FROM Table
| id | strField | numField | dateField |
|---|---|---|---|
| 1 | Bbb | 10 | 01.01.2024 |
| 2 | Abb | 10 | 04.01.2024 |
| 3 | Abb | 9 | 02.01.2024 |
| 4 | Aaa | 9 | 03.01.2024 |
| 5 | Bbb | 10 | 02.01.2024 |
| 6 | Abc | 10 | 03.01.2024 |
| 7 | Bbb | 9 | 02.01.2024 |
| 8 | Aba | 8 | 04.01.2024 |
Practice
Solve it in the editor. The query runs against a sample database in your browser.
The list of payments is stored in the Payments table.
Sort the result by sum in descending order.
Write a query and run it to see the results.
Write a query and run it to see the results.