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;
Let’s consider the Emp table below,
eid | name | age | salary |
---|---|---|---|
1 | Mike | 22 | 9000 |
2 | John | 26 | 8000 |
3 | Rerki | 21 | 6000 |
4 | Willi | 22 | 10000 |
5 | Tailor | 25 | 8000 |
SELECT * FROM Emp ORDER BY salary;
eid | name | age | salary |
---|---|---|---|
3 | Rerki | 21 | 6000 |
2 | John | 26 | 8000 |
5 | Tailor | 25 | 8000 |
1 | Mike | 22 | 9000 |
4 | Willi | 22 | 10000 |
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.
eid | name | age | salary |
---|---|---|---|
4 | Willi | 22 | 10000 |
1 | Mike | 22 | 9000 |
2 | John | 26 | 8000 |
5 | Tailor | 25 | 8000 |
3 | Rerki | 21 | 6000 |