English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Usage and examples of SQL ORDER BY keyword

SQL Keywords Reference

ORDER BY

The ORDER BY command is used to sort the result set in ascending or descending order.

By default, the ORDER BY command sorts the result set in ascending order. To sort the records in descending order, use DESC keyword.

The following SQL statement selects all columns from the 'Customers' table and sorts them by the 'CustomerName' column:

SELECT * FROM Customers
ORDER BY CustomerName;

ASC

The ASC command is used to sort the returned data in ascending order.

The following SQL statement selects all columns from the 'Customers' table and sorts them by the 'CustomerName' column:

SELECT * FROM Customers
ORDER BY CustomerName ASC;

DESC

The DESC command is used to sort the returned data in descending order.

The following SQL statement selects all columns from the 'Customers' table and sorts them in descending order by the 'CustomerName' column:

SELECT * FROM Customers
ORDER BY CustomerName DESC;

SQL Keywords Reference