SQL Order by : Keyword
SQL ORDER BY Keyword :
- SQL ORDER BY keyword is used to sort the table either in ascending order or in descending order.
- Table is sorted in Ascending order by default.
- If you need to sort the table in descending order then we can use DESC keyword.
Consider the following syntax of the SQL ORDER BY Keyword -
SELECT Column_Name(s) FROM Table_Name ORDER BY Column_Name(s) ASC or DESC
ORDER BY Example : #ASC
Consider following employee_detail table -
ID | ENAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Raj | 20 | Pune | 1000.00 |
2 | Saurabh | 20 | Pune | 6000.00 |
3 | Omkar | 24 | Mumbai | 4000.00 |
4 | Anand | 23 | Nagpur | 3000.00 |
5 | Anmol | 29 | Goa | 1000.00 |
6 | Poonam | 25 | Delhi | 9000.00 |
SQL> SELECT * FROM employee_detail ORDER BY ENAME;
Result will look like this (by default descending order) -
ID | ENAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
4 | Anand | 23 | Nagpur | 3000.00 |
5 | Anmol | 29 | Goa | 1000.00 |
3 | Omkar | 24 | Mumbai | 4000.00 |
6 | Poonam | 25 | Delhi | 9000.00 |
1 | Raj | 20 | Pune | 1000.00 |
2 | Saurabh | 20 | Pune | 6000.00 |
Consider the following query to sort the column in descending order -
SQL> SELECT * FROM employee_detail ORDER BY ENAME DESC;
ID | ENAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
2 | Saurabh | 20 | Pune | 6000.00 |
1 | Raj | 20 | Pune | 1000.00 |
6 | Poonam | 25 | Delhi | 9000.00 |
3 | Omkar | 24 | Mumbai | 4000.00 |
5 | Anmol | 29 | Goa | 1000.00 |
4 | Anand | 23 | Nagpur | 3000.00 |