The ORDER BY SQL keyword can be used in a query to tell the server in what order the query results should be returned. ORDER BY can be followed by a single column name, so that the results will be returned following the order of the data in the specified column (default is ASC: ascending). Additionally, the use of ASC and DESC keywords can be used, telling the server whether the results should be returned in ascending or descending order.
In this exemple, we'll use a table called users that contains two columns: id (the column will be the primary key and will contain integers) and a second column called username (containing strings). There will be two entries:
Here, the ORDER BY keyword tells the server to return the results from Z to A, using the username column (so regardless of the order in the id column), so the results will be:
Here, the ORDER BY keyword tells the server to return the results in an ascending order, using the id column (so regardless of the order in the username column), so the results will be:
Note: ASC is not mandatory here, as the default value for ORDER BY is ASC.