The ORDER BY clause can be used to sort the result with the respect to some column or in ascending or descending order.

Following example shows a basic syntax using ORDER BY clause:

Syntax

SELECT col1, col2,...

   FROM tbl

   ORDER BY col1 [ASC|DESC], col2 [ASC|DESC],...

The flags ASC and DESC refer to ascending and descending respectively. However, they may be omitted in which case the data will be sorted in the ascending order. The example below shows how:

SELECT * FROM Emp ORDER BY Country;

The above example will return the list of employees ordered according to their country. They will be, by default, ordered in ascending alphabetical order. In case we want to change the order to retrieve in descending order, we would need to do this:

SELECT * FROM Emp ORDER BY Country DESC;

An interesting possibility is to add more than one sorting reference to ensure that data will be sorted even if the previous column retrieves the same information. For instance, in the following example we will ask MySQL to sort data by country first, following by the city, and then by the zip code:

SELECT * FROM Emp ORDER BY Country ASC, City ASC, Zip DESC;