The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on one or more columns. Some databases sort query results in ascending order by default.
Syntax: The basic syntax of ORDER BY clause which would be used to sort result in ascending or descending order is as follows:
SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC];
You can use more than one column in the ORDER BY clause. Make sure whatever column you are using to sort, that column should be in column-list.
Example: Consider the DEPT table having the following records:
Following is an example, which would sort the result in ascending order by deptno in ascending order:
SELECT * FROM DEPT ORDER BY DEPTNO ASC;