Signup/Sign In

SQL ORDER BY Clause

Order by clause is used with SELECT statement for arranging retrieved data in sorted order. The Order by clause by default sorts the retrieved data in ascending order. To sort the data in descending order DESC keyword is used with Order by clause.


Syntax of Order By

SELECT column-list|* FROM table-name ORDER BY ASC | DESC;

Using default Order by

Consider the following Emp table,

eidnameagesalary
401Anu229000
402Shane298000
403Rohan346000
404Scott4410000
405Tiger358000
SELECT * FROM Emp ORDER BY salary;

The above query will return the resultant data in ascending order of the salary.

eidnameagesalary
403Rohan346000
402Shane298000
405Tiger358000
401Anu229000
404Scott4410000

Using Order by DESC

Consider the Emp table described above,

SELECT * FROM Emp ORDER BY salary DESC;

The above query will return the resultant data in descending order of the salary.

eidnameagesalary
404Scott4410000
401Anu229000
405Tiger358000
402Shane298000
403Rohan346000

Check out other DCL commands and their usage: