SQL Query to Order

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

Syntax of ORDER BY  in SQL

SELECT column1, column2, column3, ……
FROM table_name
ORDER BY column1, column2,column3, …… ASC|DESC;

Example of  ORDER BY Query in SQL

SQL Queries

The following SQL statement selects all customers from the “fee” table, sorted by the “T4Tutorials_RollNO” column.

SELECT * FROM fee
ORDER BY T4Tutorials_RollNO;

 

Example of ORDER the data in DESCENDING order.

The following SQL statement selects all customers from the “fee” table, sorted DESCENDING by the “T4Tutorials_RollNO” column.

SELECT * FROM fee
ORDER BY T4Tutorials_RollNO DESC;

 

How to ORDER the multiple Columns in SQL?

The following SQL statement selects all customers from the “fee” table, sorted by the “T4Tutorials_RollNO” and the “T4Tutorials_Name” column. This means that it orders by T4Tutorials_Name, but if some rows have the same T4Tutorials_Name, it orders them by T4Tutorials_RollNO:

Example of Ordering the multiple Columns in SQL

SELECT * FROM fee
ORDER BY T4Tutorials_Name, T4Tutorials_RollNO;