SQL ORDER BY Clause

The ‘Order by’ clause in SQL is used with the SELECT statement for arranging retrieved data in sorted order. The ‘order by’ clause by default sorts the retrieved data in ascending order. In other to sort the data in descending order, the DESC keyword is used with an order by clause.

 

Below is the syntax of the Order By statement.

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

 

Using default Order by

Let’s consider the Emp table below,

eidnameagesalary
1Mike229000
2John268000
3Rerki216000
4Willi2210000
5Tailor258000

 

SELECT * FROM Emp ORDER BY salary;

 

eidnameagesalary
3Rerki216000
2John268000
5Tailor258000
1Mike229000
4Willi2210000

 

Using Order by DESC

Let’s consider the Emp table described above,

SELECT * FROM Emp ORDER BY salary DESC;

 

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

eidnameagesalary
4Willi2210000
1Mike229000
2John268000
5Tailor258000
3Rerki216000