MySQL ORDER BY
The ORDER BY keyword sorts the result set in ascending or descending order. To sort records in descending order, use DESC keyword. To sort in acending order, use ASC keyword.
Syntax:
SELECT column1, column2, ..., columnN FROM tableX ORDER BY column1, column2, ..., columnN ASC|DESC;
ASC is the default option.
[st_adsense]
Example 1: Sort in ascending order
The following statement selects all clients, sorted by age:
SELECT * FROM Clients ORDER BY Age;
Example 2: Sort in descending order
The following statement selects all clients, sorted in descending order by age:
SELECT * FROM Clients ORDER BY Age DESC;
[st_adsense]
Example 3: With several columns
The following statement selects all clients, sorted by “Age” and “Address” columns:
SELECT * FROM Clients ORDER BY Age, Adress;
Example 4: With several columns
The following statement selects all clients, sorted in ascending order by “Age” and in descending order by the “Address” column:
SELECT * FROM Clients ORDER BY Age ASC, Adress DESC;
[st_adsense]