Order by clause is used with Select statement for arranging retrieved data in sorted order. The Order by clause by default sort data in ascending order. To sort 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;




Example using Order by

Consider the following Emp table,

eid
name
age
salary
401
Anu
22
9000
402
Shane
29
8000
403
Rohan
34
6000
404
Scott
44
10000
405
Tiger
35
8000
SELECT * from Emp order by salary; 

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

eid
name
age
salary
403
Rohan
34
6000
402
Shane
29
8000
405
Tiger
35
8000
401
Anu
22
9000
404
Scott
44
10000


Example of Order by DESC

Consider the Emp table described above,

SELECT * from Emp order by salary DESC;

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

eid
name
age
salary
404
Scott
44
10000
401
Anu
22
9000
405
Tiger
35
8000
402
Shane
29
8000
403
Rohan
34
6000

0 Comments:

Post a Comment