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_no | age | name | address | phone |
---|
1 | 18 | Shubham Thakur | 123 Main St, Mumbai | 9876543210 |
2 | 18 | Mohit Thakur | 321 Main St, Mumbai | 9876543201 |
3 | 19 | Abhishek | 567 New Way, Mumbai | 9876543219 |
4 | 19 | Aman Chopra | 456 Park Ave, Delhi | 9876543211 |
5 | 20 | Naveen Tulasi | 789 Broadway, Ahmedabad | 9876543212 |
6 | 21 | Aditya Arpan | 246 5th Ave, Kolkata | 9876543213 |
7 | 22 | Nishant Jain | 369 3rd St, Bengaluru | 9876543214 |
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_no | age | name | address | phone |
---|
7 | 22 | Nishant Jain | 369 3rd St, Bengaluru | 9876543214 |
6 | 21 | Aditya Arpan | 246 5th Ave, Kolkata | 9876543213 |
5 | 20 | Naveen Tulasi | 789 Broadway, Ahmedabad | 9876543212 |
4 | 19 | Aman Chopra | 456 Park Ave, Delhi | 9876543211 |
3 | 19 | Abhishek | 567 New Way, Mumbai | 9876543219 |
2 | 18 | Mohit Thakur | 321 Main St, Mumbai | 9876543201 |
1 | 18 | Shubham Thakur | 123 Main St, Mumbai | 9876543210 |
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_no | age | name | address | phone |
---|
7 | 22 | Nishant Jain | 369 3rd St, Bengaluru | 9876543214 |
6 | 21 | Aditya Arpan | 246 5th Ave, Kolkata | 9876543213 |
5 | 20 | Naveen Tulasi | 789 Broadway, Ahmedabad | 9876543212 |
3 | 19 | Abhishek | 567 New Way, Mumbai | 9876543219 |
4 | 19 | Aman Chopra | 456 Park Ave, Delhi | 9876543211 |
1 | 18 | Shubham Thakur | 123 Main St, Mumbai | 9876543210 |
2 | 18 | Mohit Thakur | 321 Main St, Mumbai | 9876543201 |
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_no | Name | Address |
---|
1 | HARSH | DELHI |
2 | PRATIK | BIHAR |
3 | RIYANKA | SILIGURI |
4 | DEEP | RAMNAGAR |
5 | SAPTARHI | KOLKATA |
6 | DHANRAJ | BARABAJAR |
7 | ROHIT | GHAZIABAD |
8 | NIRAJ | ALIPUR |
Explanation: ORDER BY 1 means sorting values according to first column in the SELECT statement.
Explore
SQL Tutorial
7 min read
Basics
Queries & Operations
SQL Joins & Functions
Data Constraints & Aggregate Functions
Advanced SQL Topics
Database Design & Security