Open In App

SQL ORDER BY

Last Updated : 25 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The ORDER BY clause in SQL is used to sort query results based on one or more columns in either ascending (ASC) or descending (DESC) order. Whether you are presenting data to users or analyzing large datasets, sorting the results in a structured way is essential.

  • By default, it sorts in ascending order (lowest to highest).
  • To sort in descending order, use the DESC keyword.

Syntax:

SELECT * FROM table_name ORDER BY column_name ASC | DESC;

Key Terms:

  • table_name: name of the table.
  • column_name: name of the column according to which the data is needed to be arranged.
  • ASC: to sort the data in ascending order.
  • DESC: to sort the data in descending order.

SQL ORDER BY Clause Examples

We have created a Student table that stores student data including their roll_no, name, age, addess, and phone. Let's look at some examples of the SQL ORDER BY clause to understand it's working in SQL. We will use the following table in examples.

roll_noagenameaddressphone
118Shubham Thakur123 Main St, Mumbai9876543210
218Mohit Thakur321 Main St, Mumbai9876543201
319Abhishek567 New Way, Mumbai9876543219
419Aman Chopra456 Park Ave, Delhi9876543211
520Naveen Tulasi789 Broadway, Ahmedabad9876543212
621Aditya Arpan246 5th Ave, Kolkata9876543213
722Nishant Jain369 3rd St, Bengaluru9876543214

Now consider the above database table and find the results of different queries.

Example 1 : Sort by a Single Column

In this example, we will fetch all data from the table Student and sort the result in descending order according to the column ROLL_NO. 

Query:

SELECT * FROM students ORDER BY ROLL_NO DESC;

Output:

roll_noagenameaddressphone
722Nishant Jain369 3rd St, Bengaluru9876543214
621Aditya Arpan246 5th Ave, Kolkata9876543213
520Naveen Tulasi789 Broadway, Ahmedabad9876543212
419Aman Chopra456 Park Ave, Delhi9876543211
319Abhishek567 New Way, Mumbai9876543219
218Mohit Thakur321 Main St, Mumbai9876543201
118Shubham Thakur123 Main St, Mumbai9876543210

In the above example, if we want to sort in ascending order we have to use ASC in place of DESC.

Example 2 : Sort by Multiple Columns

In this example, we will fetch all data from the table Student and then sort the result in descending order first according to the column age and then in ascending order according to the column name.

To sort according to multiple columns, separate the names of columns by the (,) operator. 

Query:

SELECT * FROM students ORDER BY age DESC , name ASC;

Output:

roll_noagenameaddressphone
722Nishant Jain369 3rd St, Bengaluru9876543214
621Aditya Arpan246 5th Ave, Kolkata9876543213
520Naveen Tulasi789 Broadway, Ahmedabad9876543212
319Abhishek567 New Way, Mumbai9876543219
419Aman Chopra456 Park Ave, Delhi9876543211
118Shubham Thakur123 Main St, Mumbai9876543210
218Mohit Thakur321 Main St, Mumbai9876543201

The result is first sorted by Age in descending order. For rows with the same Age, it’s further sorted by Name in ascending order.

Sorting By Column Number

Instead of using column names, you can sort results using the position of a column in the SELECT list. The number must be greater than 0 and not exceed the number of selected columns.

  • Using column numbers in ORDER BY reduces query readability.
  • Referring to columns by name is clearer and easier to understand.
  • Changing column order in SELECT doesn’t affect ORDER BY when using names.
  • Prefer column names over numbers for maintainable SQL code.

Syntax:

ORDER BY Column_Number asc/desc;

Example of Sorting By Column Number

Here we take an example to sort a database table according to column 1 i.e Roll Number. For this a query will be:

Query:

CREATE TABLE studentinfo ( Roll_no INT, NAME VARCHAR(25), Address VARCHAR(20), CONTACTNO BIGINT NOT NULL, Age INT ); INSERT INTO studentinfo VALUES (7,'ROHIT','GHAZIABAD',9193458625,18), (4,'DEEP','RAMNAGAR',9193458546,18), (1,'HARSH','DELHI',9193342625,18), (8,'NIRAJ','ALIPUR',9193678625,19), (5,'SAPTARHI','KOLKATA',9193789625,19), (2,'PRATIK','BIHAR',9193457825,19), (6,'DHANRAJ','BARABAJAR',9193358625,20), (3,'RIYANKA','SILIGURI',9193218625,20); SELECT Roll_no, Name, Address FROM studentinfo ORDER BY 1

Output:

Roll_noNameAddress
1HARSHDELHI
2PRATIKBIHAR
3RIYANKASILIGURI
4DEEPRAMNAGAR
5SAPTARHIKOLKATA
6DHANRAJBARABAJAR
7ROHITGHAZIABAD
8NIRAJALIPUR

Explanation: ORDER BY 1 means sorting values according to first column in the SELECT statement.


ORDER BY in SQL
Article Tags :

Explore