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,
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 the resultant data 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 |
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.
eid | name | age | salary |
404 | Scott | 44 | 10000 |
401 | Anu | 22 | 9000 |
405 | Tiger | 35 | 8000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
Check out other DCL commands and their usage: