ORDER BY
clause is used to sort rows of the result received from a SELECT
statement, which retrieves records from one or more tables.
ORDER BY
clause:
columns
: columns that you wish to retrievetable_name
: table that you want to retrieve records from.ORDER BY
: expression used to order the resultsASC
or DESC
: optional parameter to specify the order in which the results should be returned, either ascending or descending. Default is set to ASC
ORDER BY
in ascending orderORDER BY
clause to sort employees by their division:
ORDER BY
in descending orderemp_sal
column in descending order:
ORDER BY
with both ASC & DESC parametersORDER BY
with TEXT
data typesORDER BY
clause with these types of data, records with uppercase letters will be sorted lexicographically first, followed by records with lowercase letters.
ORDER BY
with INTERVAL
data typeinterval_data
:
ORDER BY
on INTERVAL
column will sort the values by their leading most significant time unit.
In this case months
. First are all 1 month
values, then all 2 months
values.
hours
and days
.