SQL Order by : Keyword



SQL ORDER BY Keyword :

  1. SQL ORDER BY keyword is used to sort the table either in ascending order or in descending order.
  2. Table is sorted in Ascending order by default.
  3. 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 –

IDENAMEAGEADDRESSSALARY
1Raj20Pune1000.00
2Saurabh20Pune6000.00
3Omkar24Mumbai4000.00
4Anand23Nagpur3000.00
5Anmol29Goa1000.00
6Poonam25Delhi9000.00
SQL> SELECT * FROM employee_detail ORDER BY ENAME;

Result will look like this (by default descending order) –

IDENAMEAGEADDRESSSALARY
4Anand23Nagpur3000.00
5Anmol29Goa1000.00
3Omkar24Mumbai4000.00
6Poonam25Delhi9000.00
1Raj20Pune1000.00
2Saurabh20Pune6000.00

Consider the following query to sort the column in descending order –

SQL> SELECT * FROM employee_detail ORDER BY ENAME DESC;
IDENAMEAGEADDRESSSALARY
2Saurabh20Pune6000.00
1Raj20Pune1000.00
6Poonam25Delhi9000.00
3Omkar24Mumbai4000.00
5Anmol29Goa1000.00
4Anand23Nagpur3000.00