Order By In SQL Query

Order By In SQL Query

 In this post Order By In SQL Query, we will see how to arrange data in ascending or descending order in output of a query.


ORDER BY clause is useful when we wants to arrange the query result in ascending or descending order.


General Syntax of Order By clause


Select col1, col2... from table name order by col name.

Example
Select ecode, ename, ecity from employee order by ecode;

In the output of the query you will see, ecode, ename and ecity column values , the rows will be sorted in ascending order of ecode.

You can arrange rows of result as per any column of the queried table.

You can arrange them by name of the city also. The sql query in this case will be - Select ecode, ename, ecity from employee order by ecity;

By default order by return rows in ascending oder. You can have results in descending order also. For that you have to use desc keyword with order by.

Suppose you wants output in desecending order of cities. The sql query will be like this-Select ecode, ename, ecity from employee order by ecity desc;

 For results in ascending order , you can use the keyword asc. If you donot use any of the keywords from desc and asc, by default sql will assume it as asc.