SQL ORDER BY

The ORDER BY clause in SQL is used to sort the result set of a query. By default, the sorting is done in ascending order. If you want to sort the data in descending order, you can specify it explicitly.

Syntax


SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;

        
  • column1, column2, ... – Specifies the columns by which to sort the data.
  • ASC – Sorts the data in ascending order. This is the default sorting order.
  • DESC – Sorts the data in descending order.

Example


SELECT Name, Age, Salary
FROM Employees
ORDER BY Age ASC, Salary DESC;

        

In this example:

  • The result set is first sorted by Age in ascending order.
  • Within the same Age, the data is then sorted by Salary in descending order.