Oracle: ORDER BY Clause

Sometimes in Oracle, we have a lot more data but in an unorganized form. ORDER BY is a solution to this issue. The Clause in Oracle is used to sort the data in ascending or descending order.

Syntax

Select Column from Table_name
ORDER BY Column_To_Be_Sorted asc/desc;

NOTE: By default the columns are sorted by ascending order.

Example: Sort single column through ORDER BY

We have a Stu Table in which we want to see the names arranged in ascending order.  Refer below for the Stu Table:

Stu Table

Now to Sort the Name column, Code is as follows:

Select * from Stu
Order by Name asc;

Output

Order By Clause in Oracle

Example: Sort multiple column through ORDER BY

Now, In Stu Table we want both Name and Roll_No to be sorted in descending order. The code is as follows:

Select * from stu
Order by Name desc, Roll_No desc;

Output:

Order By Clause in Oracle

Note the above output, you will find that the Name column is sorted in descending order. However, column Roll_No is not correctly sorted in Descending Order. Why? What is the reason behind this Output?

Well, this is how ORDER BY works. The column that is placed just after Order By keyword in Code is always perfectly sorted. For next column it depends that is it the independent column or not. If it is not it will sorted as shown in the output.