...
Code Block |
---|
|
SELECT firstname, surname
FROM people
WHERE country = 'Mexico' |
The SQL ORDER BY Clause
The ORDER BY clause is used to sort your columns of data by a particular ordering e.g. ascending or descending and even across multiple columns for more advanced sorting.
ORDER BY Syntax
Code Block |
---|
|
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column order |
Example Descending order
The following SQL statement selects the “firstname“ and “surname“ of all the people from the country "Mexico", in the "people" table and then sorts by surname in ascending order (default order by behaviour):
Code Block |
---|
|
SELECT firstname, surname
FROM people
WHERE country = 'Mexico'
ORDER BY surname |
You could also add the keyword ASC
but in this case, the default behaviour is ascending order.
Example Ascending order
The following SQL statement selects the “firstname“ and “surname“ of all the people from the country "Mexico", in the "people" table and then sorts by surname in desc order (the default):
Code Block |
---|
|
SELECT firstname, surname
FROM people
WHERE country = 'Mexico'
ORDER BY surname DESC |
Example Ascending order (multiple columns)
The following SQL statement selects the “firstname“ and “surname“ of all the people from the country "Mexico", in the "people" table and then sorts by surname
in DESC
(descending) order and then firstname
in ascending order (the default):
Code Block |
---|
|
SELECT firstname, surname
FROM people
WHERE country = 'Mexico'
ORDER BY surname DESC, firstname |
Info |
---|
It is also possible to reference your columns numerically int he order they appear in the select statement e.g. ORDER BY 2 DESC, 1 would achieve the same intended behaviour. |
Note |
---|
This might cause more confusion if you have a lot of columns in your SELECT statement/ |
Related articles
Filter by label (Content by label) |
---|
showLabels | false |
---|
max | 5 |
---|
spaces | com.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@9e4 |
---|
showSpace | false |
---|
sort | modified |
---|
|
...
...
...
labels | queries reporting fields tables sql |
---|
cql | label in ( "reporting" , "queries" , "tables" , "fields" , "sql" ) and type = "page" and space = "OS" |
---|
|
...