Open In App

SQL ORDER BY

Last Updated : 05 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.

The ORDER BY clause helps you organize and sort the result set of a SELECT query.

  • 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.

Student_Table

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:

Sort_Single_Column

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:

Sort_Multiple_Column

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:

sortbycolumnnumber
Sorting By Column Number

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

Tips for Using ORDER BY

TipDescription
ASC is defaultIf you don’t specify, sorting is in ascending order.
Use DESC for reverse orderSort from highest to lowest.
Supports multiple columnsSeparate by commas in the ORDER BY clause.
Can work with WHERE/GROUP BYCombine with filtering and aggregation.

ORDER BY in SQL
Article Tags :

Similar Reads