Skip to main content

ORDER BY

The ORDER BY clause in SQL is used to sort the result-set from a SELECT statement in ascending or descending order. It sorts the records in ascending order by default. If you want to sort the records in descending order, you have to use the DESC keyword.

Syntax for Ascending Order:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC;

Here, ASC is used for ascending order. If you use ORDER BY without ASC or DESC, ASC is used by default.

Syntax for Descending Order:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... DESC;

Here, DESC is used for descending order.

Usage Example

Consider the following Customers table:

IDNAMEAGEADDRESSSALARY
1Ramesh32Ahmedabad2000.0
2Khilan25Delhi1500.0
3kaushik23Kota2000.0
4Chaitali25Mumbai6500.0
5Hardik27Bhopal8500.0
6Komal22MP4500.0

Example 1 - Ascending Order:

Sort the table by the NAME column in ascending order:

SELECT * FROM Customers
ORDER BY NAME ASC;

Example 2 - Descending Order:

Sort the table by the SALARY column in descending order:

SELECT * FROM Customers
ORDER BY SALARY DESC;

Example 3 - Multiple Columns:

You can also sort by multiple columns. Sort the table by the AGE column in ascending order and then SALARY in descending order:

SELECT * FROM Customers
ORDER BY AGE ASC, SALARY DESC;

In this instance, the ORDER BY clause first sorts the Customers table by the AGE column and then sorts the sorted result further by the SALARY column.