MySQL ORDER BY Keyword

The ORDER BY keyword is used to sort the resulting records in ascending or descending order.
asc keyword - This is used to sort the records in ascending order.
desc keyword - This is used to sort the records in desceding order.

ORDER BY ascending order

Syntax for asc keyword

select column_1, column_2, ...  from table_name ORDER BY asc;

Example

select * from employee ORDER BY name asc;

By default, the ORDER BY will return records in ascending order. So the above query can be written like,

select * from employee ORDER BY name;

ORDER BY descending order

Syntax for desc keyword

select column_1, column_2, ...  from table_name ORDER BY desc;

Example

select * from employee ORDER BY name desc;

This will return the records with names sorted in descending order.

Its also possible to sort in multiple orders in a single query.

SELECT * FROM employee ORDER BY location ASC, name DESC;

The above query will return the records with location sorted in ascending order and then the name in descending order.


Most Read